Skip to content
Permalink
Browse files
fix: The backend store of 'xx' has not been initialized (#708)
The backend has been initialized, but this error is still raised.

fix: #632

Change-Id: I9890559365c01bd73bc85c25e1cb058c2637fb0d
  • Loading branch information
javeme authored and Linary committed Nov 4, 2019
1 parent 95eba35 commit 5878d8f61d39b6da8c7d3120e4ceb2b066d04ad7
Show file tree
Hide file tree
Showing 31 changed files with 405 additions and 183 deletions.
@@ -192,14 +192,15 @@ private void loadGraph(String name, String path) {

private void checkBackendVersionOrExit() {
for (String graph : this.graphs()) {
// TODO: close tx from main thread
HugeGraph hugegraph = this.graph(graph);
boolean persistence = hugegraph.graphTransaction().store()
.features().supportsPersistence();
if (!persistence) {
hugegraph.initBackend();
}
BackendStoreSystemInfo info = new BackendStoreSystemInfo(hugegraph);
if (!info.exist()) {
if (!info.exists()) {
throw new BackendException(
"The backend store of '%s' has not been initialized",
hugegraph.name());
@@ -124,20 +124,10 @@ protected synchronized void doClose() {
}
}

public final void checkClusterConnected() {
public final boolean clusterConnected() {
E.checkState(this.cluster != null,
"Cassandra cluster has not been initialized");
E.checkState(!this.cluster.isClosed(),
"Cassandra cluster has been closed");
}

public final void checkSessionConnected() {
this.checkClusterConnected();

E.checkState(this.session() != null,
"Cassandra session has not been initialized");
E.checkState(!this.session().closed(),
"Cassandra session has been closed");
return !this.cluster.isClosed();
}

/**
@@ -40,10 +40,12 @@
import com.baidu.hugegraph.backend.store.BackendMutation;
import com.baidu.hugegraph.backend.store.BackendStoreProvider;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.exception.ConnectionException;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.Statement;
import com.datastax.driver.core.exceptions.DriverException;
@@ -152,7 +154,7 @@ public synchronized void open(HugeConfig config) {
} catch (Throwable e2) {
LOG.warn("Failed to close cluster after an error", e2);
}
throw e;
throw new ConnectionException("Failed to connect to Cassandra", e);
}

LOG.debug("Store opened: {}", this.store);
@@ -164,13 +166,19 @@ public void close() {
this.sessions.close();
}

@Override
public boolean opened() {
this.checkClusterConnected();
return !this.sessions.session().closed();
}

@Override
public void mutate(BackendMutation mutation) {
if (LOG.isDebugEnabled()) {
LOG.debug("Store {} mutation: {}", this.store, mutation);
}

this.checkSessionConnected();
this.checkOpened();
CassandraSessionPool.Session session = this.sessions.session();

for (Iterator<BackendAction> it = mutation.mutation(); it.hasNext();) {
@@ -236,7 +244,7 @@ private void mutate(CassandraSessionPool.Session session,

@Override
public Iterator<BackendEntry> query(Query query) {
this.checkSessionConnected();
this.checkOpened();

CassandraTable table = this.table(CassandraTable.tableType(query));
return table.query(this.sessions.session(), query);
@@ -265,7 +273,7 @@ public void init() {
}

// Create tables
this.checkSessionConnected();
this.checkOpened();
this.initTables();

LOG.debug("Store initialized: {}", this.store);
@@ -276,25 +284,40 @@ public void clear() {
this.checkClusterConnected();

if (this.existsKeyspace()) {
this.checkSessionConnected();
this.checkOpened();
this.clearTables();
this.clearKeyspace();
}

LOG.debug("Store cleared: {}", this.store);
}

@Override
public boolean initialized() {
this.checkClusterConnected();

if (!this.existsKeyspace()) {
return false;
}
for (CassandraTable table : this.tables()) {
if (!this.existsTable(table.table())) {
return false;
}
}
return true;
}

@Override
public void truncate() {
this.checkSessionConnected();
this.checkOpened();

this.truncateTables();
LOG.debug("Store truncated: {}", this.store);
}

@Override
public void beginTx() {
this.checkSessionConnected();
this.checkOpened();

CassandraSessionPool.Session session = this.sessions.session();
if (session.txState() != TxState.CLEAN) {
@@ -306,7 +329,7 @@ public void beginTx() {

@Override
public void commitTx() {
this.checkSessionConnected();
this.checkOpened();

CassandraSessionPool.Session session = this.sessions.session();
if (session.txState() != TxState.BEGIN) {
@@ -345,7 +368,7 @@ public void commitTx() {

@Override
public void rollbackTx() {
this.checkSessionConnected();
this.checkOpened();

CassandraSessionPool.Session session = this.sessions.session();

@@ -370,6 +393,19 @@ protected Cluster cluster() {
return this.sessions.cluster();
}

protected boolean existsKeyspace() {
return this.cluster().getMetadata().getKeyspace(this.keyspace) != null;
}

protected boolean existsTable(String table) {
KeyspaceMetadata keyspace = this.cluster().getMetadata()
.getKeyspace(this.keyspace);
if (keyspace != null && keyspace.getTable(table) != null) {
return true;
}
return false;
}

protected void initKeyspace() {
Statement stmt = SchemaBuilder.createKeyspace(this.keyspace)
.ifNotExists().with()
@@ -446,10 +482,6 @@ protected void clearKeyspace() {
}
}

protected boolean existsKeyspace() {
return this.cluster().getMetadata().getKeyspace(this.keyspace) != null;
}

protected void initTables() {
CassandraSessionPool.Session session = this.sessions.session();
for (CassandraTable table : this.tables()) {
@@ -487,28 +519,17 @@ protected final CassandraTable table(HugeType type) {

@Override
protected CassandraSessionPool.Session session(HugeType type) {
this.checkSessionConnected();
this.checkOpened();
return this.sessions.session();
}

protected final void checkClusterConnected() {
E.checkState(this.sessions != null,
"Cassandra store has not been initialized");
this.sessions.checkClusterConnected();
}

protected final void checkSessionConnected() {
E.checkState(this.sessions != null,
"Cassandra store has not been initialized");
try {
this.sessions.checkSessionConnected();
} catch (DriverException e) {
throw new BackendException("Can't connect to cassandra", e);
}
E.checkState(this.sessions != null && this.sessions.clusterConnected(),
"Cassandra cluster has not been connected");
}

protected static final CassandraBackendEntry castBackendEntry(
BackendEntry entry) {
BackendEntry entry) {
assert entry instanceof CassandraBackendEntry : entry.getClass();
if (!(entry instanceof CassandraBackendEntry)) {
throw new BackendException(
@@ -548,14 +569,14 @@ protected Collection<CassandraTable> tables() {

@Override
public void increaseCounter(HugeType type, long increment) {
this.checkSessionConnected();
this.checkOpened();
CassandraSessionPool.Session session = super.sessions.session();
this.counters.increaseCounter(session, type, increment);
}

@Override
public long getCounter(HugeType type) {
this.checkSessionConnected();
this.checkOpened();
CassandraSessionPool.Session session = super.sessions.session();
return this.counters.getCounter(session, type);
}
@@ -71,6 +71,11 @@ public void close() {
this.store.close();
}

@Override
public boolean opened() {
return this.store.opened();
}

@Override
public void init() {
this.store.init();
@@ -81,6 +86,11 @@ public void clear() {
this.store.clear();
}

@Override
public boolean initialized() {
return this.store.initialized();
}

@Override
public void truncate() {
this.store.truncate();
@@ -178,7 +178,7 @@ public static <T> Iterator<T> emptyIterator() {
return (Iterator<T>) EMPTY_ITERATOR;
}

private static class EmptyIterator<E> implements Iterator<E>,
private static class EmptyIterator<T> implements Iterator<T>,
Metadatable {
@Override
public Object metadata(String meta, Object... args) {
@@ -191,7 +191,7 @@ public boolean hasNext() {
}

@Override
public E next() {
public T next() {
throw new NoSuchElementException();
}
}
@@ -19,6 +19,7 @@

package com.baidu.hugegraph.backend.store;

import com.baidu.hugegraph.exception.ConnectionException;
import com.baidu.hugegraph.type.HugeType;

public abstract class AbstractBackendStore<Session extends BackendSession>
@@ -52,6 +53,14 @@ public <R> R metadata(HugeType type, String meta, Object[] args) {
return dispatcher.dispatchMetaHandler(session, meta, args);
}

protected void checkOpened() throws ConnectionException {
if (!this.opened()) {
throw new ConnectionException(
"The '%s' store of %s has not been opened",
this.database(), this.provider().type());
}
}

@Override
public String toString() {
return String.format("%s/%s", this.database(), this.store());
@@ -42,10 +42,12 @@ public interface BackendStore {
// Open/close database
public void open(HugeConfig config);
public void close();
public boolean opened();

// Initialize/clear database
public void init();
public void clear();
public boolean initialized();

// Delete all data of database (keep table structure)
public void truncate();
@@ -19,14 +19,12 @@

package com.baidu.hugegraph.backend.store;

import java.io.UncheckedIOException;
import java.util.Map;

import org.apache.tinkerpop.gremlin.structure.Graph.Hidden;
import org.slf4j.Logger;

import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.BackendException;
import com.baidu.hugegraph.backend.tx.SchemaTransaction;
import com.baidu.hugegraph.schema.PropertyKey;
import com.baidu.hugegraph.schema.SchemaElement;
@@ -64,17 +62,14 @@ public void init() {

private Map<String, Object> info() {
SchemaTransaction schema = this.graph.schemaTransaction();
PropertyKey pkey = null;
try {
pkey = schema.getPropertyKey(PK_BACKEND_INFO);
} catch (BackendException | IllegalStateException |
UncheckedIOException ignored) {
// pass
}
PropertyKey pkey = schema.getPropertyKey(PK_BACKEND_INFO);
return pkey != null ? pkey.userdata() : null;
}

public boolean exist() {
public boolean exists() {
if (!this.graph.schemaTransaction().store().initialized()) {
return false;
}
return this.info() != null;
}

@@ -215,6 +215,16 @@ public BackendFeatures features() {
return FEATURES;
}

@Override
public boolean opened() {
return true;
}

@Override
public boolean initialized() {
return true;
}

/***************************** Store defines *****************************/

public static class InMemorySchemaStore extends InMemoryDBStore {

0 comments on commit 5878d8f

Please sign in to comment.