Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hotfix] Fix stack overflow exception for cluster metadata in client #329

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -314,15 +314,14 @@ private static Cluster initializeCluster(Configuration conf, RpcClient rpcClient
}

/** Invalid the bucket metadata for the given physical table paths. */
public void invalidPhysicalTableBucketMeta(
Collection<PhysicalTablePath> physicalTablesToInvalid) {
public void invalidPhysicalTableBucketMeta(Set<PhysicalTablePath> physicalTablesToInvalid) {
if (!physicalTablesToInvalid.isEmpty()) {
cluster = cluster.invalidPhysicalTableBucketMeta(physicalTablesToInvalid);
}
}

/** Get the table physical paths by table ids and partition ids. */
public Collection<PhysicalTablePath> getPhysicalTablePathByIds(
public Set<PhysicalTablePath> getPhysicalTablePathByIds(
@Nullable Collection<Long> tableId,
@Nullable Collection<TablePartition> tablePartitions) {
Set<PhysicalTablePath> physicalTablePaths = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -270,7 +269,7 @@ private synchronized void handleFetchLogResponse(
// if is invalid metadata exception, we need to clear table bucket meta
// to enable another round of log fetch to request new medata
if (e instanceof InvalidMetadataException) {
Collection<PhysicalTablePath> physicalTablePaths =
Set<PhysicalTablePath> physicalTablePaths =
metadataUpdater.getPhysicalTablePathByIds(
tableOrPartitionsInFetchRequest.tableIds,
tableOrPartitionsInFetchRequest.tablePartitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

import javax.annotation.Nullable;

import java.util.Arrays;
import java.util.Objects;

/** This is used to describe per-bucket location information. */
@Internal
public final class BucketLocation {
Expand Down Expand Up @@ -73,6 +76,26 @@ public ServerNode[] getReplicas() {
return replicas;
}

@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (!(object instanceof BucketLocation)) {
return false;
}
BucketLocation that = (BucketLocation) object;
return Objects.equals(physicalTablePath, that.physicalTablePath)
&& Objects.equals(tableBucket, that.tableBucket)
&& Objects.equals(leader, that.leader)
&& Objects.deepEquals(replicas, that.replicas);
}

@Override
public int hashCode() {
return Objects.hash(physicalTablePath, tableBucket, leader, Arrays.hashCode(replicas));
}

@Override
public String toString() {
return String.format(
Expand Down
19 changes: 13 additions & 6 deletions fluss-common/src/main/java/com/alibaba/fluss/cluster/Cluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import javax.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
* An immutable representation of a subset of the server nodes, tables, and buckets and schemas in
Expand Down Expand Up @@ -122,12 +122,19 @@ public Cluster(
this.pathByTableId = Collections.unmodifiableMap(tempPathByTableId);
}

public Cluster invalidPhysicalTableBucketMeta(
Collection<PhysicalTablePath> physicalTablesToInvalid) {
public Cluster invalidPhysicalTableBucketMeta(Set<PhysicalTablePath> physicalTablesToInvalid) {
// should remove invalid tables from current availableLocationsByPath
Map<PhysicalTablePath, List<BucketLocation>> newBucketLocationsByPath =
new HashMap<>(availableLocationsByPath);
for (PhysicalTablePath path : physicalTablesToInvalid) {
newBucketLocationsByPath.remove(path);
new HashMap<>(availableLocationsByPath.size() - physicalTablesToInvalid.size());
// copy the metadata from current availableLocationsByPath to newBucketLocationsByPath
// except for the tables in physicalTablesToInvalid
for (Map.Entry<PhysicalTablePath, List<BucketLocation>> tablePathAndBucketLocations :
availableLocationsByPath.entrySet()) {
if (!physicalTablesToInvalid.contains(tablePathAndBucketLocations.getKey())) {
newBucketLocationsByPath.put(
tablePathAndBucketLocations.getKey(),
new ArrayList<>(tablePathAndBucketLocations.getValue()));
}
}
return new Cluster(
new HashMap<>(aliveTabletServersById),
Expand Down
110 changes: 52 additions & 58 deletions fluss-common/src/test/java/com/alibaba/fluss/cluster/ClusterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,49 +68,7 @@ void setup() {

@Test
void testReturnModifiableCollections() {
Map<PhysicalTablePath, List<BucketLocation>> tablePathToBucketLocations = new HashMap<>();
tablePathToBucketLocations.put(
DATA1_PHYSICAL_TABLE_PATH,
Arrays.asList(
new BucketLocation(
DATA1_PHYSICAL_TABLE_PATH, DATA1_TABLE_ID, 0, NODES[0], NODES),
new BucketLocation(
DATA1_PHYSICAL_TABLE_PATH, DATA1_TABLE_ID, 1, null, NODES),
new BucketLocation(
DATA1_PHYSICAL_TABLE_PATH, DATA1_TABLE_ID, 2, NODES[2], NODES)));
tablePathToBucketLocations.put(
PhysicalTablePath.of(DATA2_TABLE_PATH),
Arrays.asList(
new BucketLocation(
PhysicalTablePath.of(DATA2_TABLE_PATH),
DATA2_TABLE_ID,
0,
null,
NODES),
new BucketLocation(
PhysicalTablePath.of(DATA2_TABLE_PATH),
DATA2_TABLE_ID,
1,
NODES[0],
NODES)));

Map<TablePath, Long> tablePathToTableId = new HashMap<>();
tablePathToTableId.put(DATA1_TABLE_PATH, DATA1_TABLE_ID);
tablePathToTableId.put(DATA2_TABLE_PATH, DATA2_TABLE_ID);

Map<TablePath, TableInfo> tablePathToTableInfo = new HashMap<>();
tablePathToTableInfo.put(DATA1_TABLE_PATH, DATA1_TABLE_INFO);
tablePathToTableInfo.put(DATA2_TABLE_PATH, DATA2_TABLE_INFO);

Cluster cluster =
new Cluster(
aliveTabletServersById,
COORDINATOR_SERVER,
tablePathToBucketLocations,
tablePathToTableId,
Collections.emptyMap(),
tablePathToTableInfo);

Cluster cluster = createCluster();
assertThatThrownBy(() -> cluster.getAliveTabletServers().put(1, NODES[3]))
.isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(
Expand All @@ -129,6 +87,50 @@ void testReturnModifiableCollections() {

@Test
void testGetTable() {
Cluster cluster = createCluster();
assertThat(cluster.getTable(DATA1_TABLE_PATH).get()).isEqualTo(DATA1_TABLE_INFO);
assertThat(cluster.getTable(DATA2_TABLE_PATH).get()).isEqualTo(DATA2_TABLE_INFO);
assertThat(cluster.getSchema(DATA1_TABLE_PATH).get())
.isEqualTo(new SchemaInfo(DATA1_SCHEMA, 1));
assertThat(cluster.getSchema(DATA2_TABLE_PATH).get())
.isEqualTo(new SchemaInfo(DATA2_SCHEMA, 1));
}

@Test
void testInvalidMetaAndUpdate() {
Cluster cluster = createCluster();
for (int i = 0; i < 10000; i++) {
// mock invalid meta
cluster =
cluster.invalidPhysicalTableBucketMeta(
Collections.singleton(DATA1_PHYSICAL_TABLE_PATH));
// mock update meta
cluster =
new Cluster(
aliveTabletServersById,
COORDINATOR_SERVER,
new HashMap<>(cluster.getBucketLocationsByPath()),
new HashMap<>(cluster.getTableIdByPath()),
Collections.emptyMap(),
new HashMap<>(cluster.getTableInfoByPath()));
}

// verify available buckets
List<BucketLocation> availableBuckets =
cluster.getAvailableBucketsForPhysicalTablePath(
PhysicalTablePath.of(DATA2_TABLE_PATH));
assertThat(availableBuckets)
.isEqualTo(
Collections.singletonList(
new BucketLocation(
PhysicalTablePath.of(DATA2_TABLE_PATH),
DATA2_TABLE_ID,
1,
NODES[0],
NODES)));
}

private Cluster createCluster() {
Map<PhysicalTablePath, List<BucketLocation>> tablePathToBucketLocations = new HashMap<>();
tablePathToBucketLocations.put(
DATA1_PHYSICAL_TABLE_PATH,
Expand Down Expand Up @@ -163,20 +165,12 @@ void testGetTable() {
tablePathToTableInfo.put(DATA1_TABLE_PATH, DATA1_TABLE_INFO);
tablePathToTableInfo.put(DATA2_TABLE_PATH, DATA2_TABLE_INFO);

Cluster cluster =
new Cluster(
aliveTabletServersById,
COORDINATOR_SERVER,
tablePathToBucketLocations,
tablePathToTableId,
Collections.emptyMap(),
tablePathToTableInfo);

assertThat(cluster.getTable(DATA1_TABLE_PATH).get()).isEqualTo(DATA1_TABLE_INFO);
assertThat(cluster.getTable(DATA2_TABLE_PATH).get()).isEqualTo(DATA2_TABLE_INFO);
assertThat(cluster.getSchema(DATA1_TABLE_PATH).get())
.isEqualTo(new SchemaInfo(DATA1_SCHEMA, 1));
assertThat(cluster.getSchema(DATA2_TABLE_PATH).get())
.isEqualTo(new SchemaInfo(DATA2_SCHEMA, 1));
return new Cluster(
aliveTabletServersById,
COORDINATOR_SERVER,
tablePathToBucketLocations,
tablePathToTableId,
Collections.emptyMap(),
tablePathToTableInfo);
}
}