Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
*/
package io.trino.plugin.memory;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.airlift.json.JsonCodec;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;
Expand All @@ -24,33 +22,14 @@
import static io.airlift.json.JsonCodec.jsonCodec;
import static java.util.Objects.requireNonNull;

public class MemoryDataFragment
public record MemoryDataFragment(HostAddress hostAddress, long rows)
{
private static final JsonCodec<MemoryDataFragment> MEMORY_DATA_FRAGMENT_CODEC = jsonCodec(MemoryDataFragment.class);

private final HostAddress hostAddress;
private final long rows;

@JsonCreator
public MemoryDataFragment(
@JsonProperty("hostAddress") HostAddress hostAddress,
@JsonProperty("rows") long rows)
public MemoryDataFragment
{
this.hostAddress = requireNonNull(hostAddress, "hostAddress is null");
requireNonNull(hostAddress, "hostAddress is null");
checkArgument(rows >= 0, "Rows number cannot be negative");
this.rows = rows;
}

@JsonProperty
public HostAddress getHostAddress()
{
return hostAddress;
}

@JsonProperty
public long getRows()
{
return rows;
}

public Slice toSlice()
Expand All @@ -65,7 +44,7 @@ public static MemoryDataFragment fromSlice(Slice fragment)

public static MemoryDataFragment merge(MemoryDataFragment a, MemoryDataFragment b)
{
checkArgument(a.getHostAddress().equals(b.getHostAddress()), "Cannot merge fragments from different hosts");
return new MemoryDataFragment(a.getHostAddress(), a.getRows() + b.getRows());
checkArgument(a.hostAddress().equals(b.hostAddress()), "Cannot merge fragments from different hosts");
return new MemoryDataFragment(a.hostAddress(), a.rows() + b.rows());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,18 @@
*/
package io.trino.plugin.memory;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableSet;
import io.trino.spi.connector.ConnectorInsertTableHandle;

import java.util.Set;

import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;

public class MemoryInsertTableHandle
public record MemoryInsertTableHandle(long table, Set<Long> activeTableIds)
implements ConnectorInsertTableHandle
{
private final long table;
private final Set<Long> activeTableIds;

@JsonCreator
public MemoryInsertTableHandle(
@JsonProperty("table") long table,
@JsonProperty("activeTableIds") Set<Long> activeTableIds)
{
this.table = table;
this.activeTableIds = requireNonNull(activeTableIds, "activeTableIds is null");
}

@JsonProperty
public long getTable()
{
return table;
}

@JsonProperty
public Set<Long> getActiveTableIds()
{
return activeTableIds;
}

@Override
public String toString()
public MemoryInsertTableHandle
{
return toStringHelper(this)
.add("table", table)
.add("activeTableIds", activeTableIds)
.toString();
activeTableIds = ImmutableSet.copyOf(requireNonNull(activeTableIds, "activeTableIds is null"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public synchronized Optional<ConnectorOutputMetadata> finishInsert(
requireNonNull(insertHandle, "insertHandle is null");
MemoryInsertTableHandle memoryInsertHandle = (MemoryInsertTableHandle) insertHandle;

updateRowsOnHosts(memoryInsertHandle.getTable(), fragments);
updateRowsOnHosts(memoryInsertHandle.table(), fragments);
return Optional.empty();
}

Expand Down Expand Up @@ -477,7 +477,7 @@ private void updateRowsOnHosts(long tableId, Collection<Slice> fragments)
Map<HostAddress, MemoryDataFragment> dataFragments = new HashMap<>(info.dataFragments());
for (Slice fragment : fragments) {
MemoryDataFragment memoryDataFragment = MemoryDataFragment.fromSlice(fragment);
dataFragments.merge(memoryDataFragment.getHostAddress(), memoryDataFragment, MemoryDataFragment::merge);
dataFragments.merge(memoryDataFragment.hostAddress(), memoryDataFragment, MemoryDataFragment::merge);
}

tables.put(tableId, new TableInfo(tableId, info.schemaName(), info.tableName(), info.columns(), dataFragments, info.comment()));
Expand All @@ -493,7 +493,7 @@ public TableStatistics getTableStatistics(ConnectorSession session, ConnectorTab
{
List<MemoryDataFragment> dataFragments = getDataFragments(((MemoryTableHandle) tableHandle).id());
long rows = dataFragments.stream()
.mapToLong(MemoryDataFragment::getRows)
.mapToLong(MemoryDataFragment::rows)
.sum();
return TableStatistics.builder()
.setRowCount(Estimate.of(rows))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public ConnectorPageSink createPageSink(ConnectorTransactionHandle transactionHa
public ConnectorPageSink createPageSink(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorInsertTableHandle insertTableHandle, ConnectorPageSinkId pageSinkId)
{
MemoryInsertTableHandle memoryInsertTableHandle = (MemoryInsertTableHandle) insertTableHandle;
long tableId = memoryInsertTableHandle.getTable();
checkState(memoryInsertTableHandle.getActiveTableIds().contains(tableId));
long tableId = memoryInsertTableHandle.table();
checkState(memoryInsertTableHandle.activeTableIds().contains(tableId));

pagesStore.cleanUp(memoryInsertTableHandle.getActiveTableIds());
pagesStore.cleanUp(memoryInsertTableHandle.activeTableIds());
pagesStore.initialize(tableId);
return new MemoryPageSink(pagesStore, currentHostAddress, tableId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ public ConnectorSplitSource getSplits(
ImmutableList.Builder<ConnectorSplit> splits = ImmutableList.builder();

for (MemoryDataFragment dataFragment : dataFragments) {
long rows = dataFragment.getRows();
long rows = dataFragment.rows();
totalRows += rows;

if (table.limit().isPresent() && totalRows > table.limit().getAsLong()) {
rows -= totalRows - table.limit().getAsLong();
splits.add(new MemorySplit(table.id(), 0, 1, dataFragment.getHostAddress(), rows, OptionalLong.of(rows)));
splits.add(new MemorySplit(table.id(), 0, 1, dataFragment.hostAddress(), rows, OptionalLong.of(rows)));
break;
}

for (int i = 0; i < splitsPerNode; i++) {
splits.add(new MemorySplit(table.id(), i, splitsPerNode, dataFragment.getHostAddress(), rows, OptionalLong.empty()));
splits.add(new MemorySplit(table.id(), i, splitsPerNode, dataFragment.hostAddress(), rows, OptionalLong.empty()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.plugin.memory;

import com.google.common.collect.ImmutableMap;
import io.airlift.units.DataSize;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static io.airlift.configuration.testing.ConfigAssertions.assertFullMapping;
import static io.airlift.configuration.testing.ConfigAssertions.assertRecordedDefaults;
import static io.airlift.configuration.testing.ConfigAssertions.recordDefaults;
import static io.airlift.units.DataSize.Unit.GIGABYTE;
import static io.airlift.units.DataSize.Unit.MEGABYTE;

final class TestMemoryConfig
{
@Test
void testDefaults()
{
assertRecordedDefaults(recordDefaults(MemoryConfig.class)
.setSplitsPerNode(Runtime.getRuntime().availableProcessors())
.setMaxDataPerNode(DataSize.of(128, MEGABYTE))
.setEnableLazyDynamicFiltering(true));
}

@Test
void testExplicitPropertyMappings()
{
Map<String, String> properties = ImmutableMap.<String, String>builder()
.put("memory.splits-per-node", "100")
.put("memory.max-data-per-node", "1GB")
.put("memory.enable-lazy-dynamic-filtering", "false")
.buildOrThrow();

MemoryConfig expected = new MemoryConfig()
.setSplitsPerNode(100)
.setMaxDataPerNode(DataSize.of(1, GIGABYTE))
.setEnableLazyDynamicFiltering(false);

assertFullMapping(properties, expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void testActiveTableIds()
MemoryTableHandle firstTableHandle = (MemoryTableHandle) metadata.getTableHandle(SESSION, firstTableName, Optional.empty(), Optional.empty());
long firstTableId = firstTableHandle.id();

assertThat(metadata.beginInsert(SESSION, firstTableHandle, ImmutableList.of(), NO_RETRIES).getActiveTableIds()).contains(firstTableId);
assertThat(metadata.beginInsert(SESSION, firstTableHandle, ImmutableList.of(), NO_RETRIES).activeTableIds()).contains(firstTableId);

SchemaTableName secondTableName = new SchemaTableName("default", "second_table");
metadata.createTable(SESSION, new ConnectorTableMetadata(secondTableName, ImmutableList.of(), ImmutableMap.of()), false);
Expand All @@ -115,8 +115,8 @@ public void testActiveTableIds()

assertThat(firstTableId)
.isNotEqualTo(secondTableId);
assertThat(metadata.beginInsert(SESSION, secondTableHandle, ImmutableList.of(), NO_RETRIES).getActiveTableIds()).contains(firstTableId);
assertThat(metadata.beginInsert(SESSION, secondTableHandle, ImmutableList.of(), NO_RETRIES).getActiveTableIds()).contains(secondTableId);
assertThat(metadata.beginInsert(SESSION, secondTableHandle, ImmutableList.of(), NO_RETRIES).activeTableIds()).contains(firstTableId);
assertThat(metadata.beginInsert(SESSION, secondTableHandle, ImmutableList.of(), NO_RETRIES).activeTableIds()).contains(secondTableId);
}

@Test
Expand Down