Skip to content
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 @@ -74,18 +74,31 @@ public class HstoreSessionsImpl extends HstoreSessions {
private static volatile Boolean initializedNode = Boolean.FALSE;
private static volatile PDClient defaultPdClient;
private static volatile HgStoreClient hgStoreClient;
private final GraphStoreClient graphStoreClient;
private final HugeConfig config;
private final HstoreSession session;
private final Map<String, Integer> tables;
private final AtomicInteger refCount;
private final String graphName;

public HstoreSessionsImpl(HugeConfig config, String database, String store) {
this(config, database, store, null);
}

HstoreSessionsImpl(HugeConfig config, String database, String store,
GraphStoreClient graphStoreClient) {
super(config, database, store);
this.config = config;
this.graphName = database + "/" + store;
this.initStoreNode(config);
this.session = new HstoreSession(this.config, graphName);
if (graphStoreClient == null) {
this.initStoreNode(config);
this.graphStoreClient = new GraphStoreClient();
} else {
this.graphStoreClient = graphStoreClient;
}
this.session = new HstoreSession(
this.config, this.graphName,
this.graphStoreClient.openSession(this.graphName));
this.tables = new ConcurrentHashMap<>();
this.refCount = new AtomicInteger(1);
}
Expand Down Expand Up @@ -130,10 +143,11 @@ public void open() throws Exception {
E.checkArgument(partitionCount > -1,
"The value of hstore.partition_count " +
"cannot be less than 0.");
defaultPdClient.setGraph(Metapb.Graph.newBuilder()
.setGraphName(this.graphName)
.setPartitionCount(partitionCount)
.build());
this.graphStoreClient.setGraph(
Metapb.Graph.newBuilder()
.setGraphName(this.graphName)
.setPartitionCount(partitionCount)
.build());
infoInitializedGraph.add(this.graphName);
}
}
Expand Down Expand Up @@ -181,20 +195,27 @@ public void truncateTable(String table) {
public void clear() {
this.session.deleteGraph();
try {
hgStoreClient.getPdClient().delGraph(this.graphName);
this.graphStoreClient.delGraph(this.graphName);
} catch (PDException ignored) {

} finally {
clearInitializedGraph(this.graphName);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ The production-level clear/reopen path is still untested and can leave the graph unregistered. HstoreStore.open() returns through sessions.useSession() when its session pool is still open, while BackendSessionPool.useSession() only reattaches/detects the existing session and never calls HstoreSessionsImpl.open() or setGraph(). Thus clear(true) followed by store.open(config) can delete the PD graph and then skip recreating it; the new test calls HstoreSessionsImpl.open() directly and does not cover this path. Please make the store-level reopen re-register the graph and add a HstoreStore clear → open regression test.

}
}

private static void clearInitializedGraph(String graphName) {
infoInitializedGraph.remove(graphName);
}

@Override
public final Session session() {
return (Session) super.getOrNewSession();
}

@Override
protected final Session newSession() {
return new HstoreSession(this.config(), this.graphName);
return new HstoreSession(this.config(), this.graphName,
this.graphStoreClient.openSession(this.graphName));
}

@Override
Expand All @@ -216,6 +237,21 @@ protected synchronized void doClose() {
private void checkValid() {
}

static class GraphStoreClient {

HgStoreSession openSession(String graphName) {
return hgStoreClient.openSession(graphName);
}

void setGraph(Metapb.Graph graph) throws PDException {
defaultPdClient.setGraph(graph);
}

void delGraph(String graphName) throws PDException {
hgStoreClient.getPdClient().delGraph(graphName);
}
}

private static class ColumnIterator<T extends HgKvIterator> implements
BackendColumnIterator,
Countable {
Expand Down Expand Up @@ -418,10 +454,11 @@ private final class HstoreSession extends Session {
private final HgStoreSession graph;
int changedSize = 0;

public HstoreSession(HugeConfig conf, String graphName) {
public HstoreSession(HugeConfig conf, String graphName,
HgStoreSession graph) {
setGraphName(graphName);
setConf(conf);
this.graph = hgStoreClient.openSession(graphName);
this.graph = graph;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Proxy;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.pd.common.PDException;
import org.apache.hugegraph.pd.grpc.Metapb;
import org.apache.hugegraph.store.HgStoreSession;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -36,4 +43,61 @@ public void testProductionClassDoesNotReferenceTestAssert() throws IOException {
"org/apache/hugegraph/testutil/Assert"));
}
}

@Test
public void testClearAndRecreateGraphRepeatedly() throws Exception {
String graphName = "hugegraph/hstore-clear-test";
AtomicInteger graphRegistrations = new AtomicInteger();
AtomicInteger storeGraphDeletes = new AtomicInteger();
AtomicInteger pdGraphDeletes = new AtomicInteger();
HgStoreSession storeSession = (HgStoreSession) Proxy.newProxyInstance(
HgStoreSession.class.getClassLoader(),
new Class<?>[]{HgStoreSession.class},
(proxy, method, args) -> {
Assert.assertEquals("deleteGraph", method.getName());
Assert.assertArrayEquals(new Object[]{graphName}, args);
storeGraphDeletes.incrementAndGet();
return true;
});
HstoreSessionsImpl.GraphStoreClient client =
new HstoreSessionsImpl.GraphStoreClient() {
@Override
HgStoreSession openSession(String name) {
Assert.assertEquals(graphName, name);
return storeSession;
}

@Override
void setGraph(Metapb.Graph graph) {
Assert.assertEquals(graphName, graph.getGraphName());
graphRegistrations.incrementAndGet();
}

@Override
void delGraph(String name) throws PDException {
Assert.assertEquals(graphName, name);
pdGraphDeletes.incrementAndGet();
throw new PDException(0, "exercise clear() finally block");
}
};
HugeConfig config = new HugeConfig(Collections.emptyMap());
HstoreSessionsImpl sessions = new HstoreSessionsImpl(
config, "hugegraph", "hstore-clear-test", client);

try {
sessions.open();
Assert.assertEquals(1, graphRegistrations.get());

for (int i = 0; i < 50; i++) {
sessions.clear();
sessions.open();
Assert.assertEquals(i + 2, graphRegistrations.get());
}

Assert.assertEquals(50, storeGraphDeletes.get());
Assert.assertEquals(50, pdGraphDeletes.get());
} finally {
sessions.clear();
}
}
}
Loading