Skip to content
Permalink
Browse files
fix connect failed of mysql backend when using studio gremlin (#765)
fixed: #758

Change-Id: Ia52ea1588b1b4ecef882732cfbb8606ceb4bea30
  • Loading branch information
zhoney authored and javeme committed Dec 6, 2019
1 parent cfb88da commit 17742425797f76000775a5cb0081639837218f26
Showing 22 changed files with 438 additions and 165 deletions.
@@ -209,21 +209,27 @@ private void tryOpen() {
} catch (InvalidQueryException ignored) {}
}

@Override
public void open() {
assert this.session == null;
this.session = cluster().connect(keyspace());
this.opened = true;
}

@Override
public boolean opened() {
return this.session != null;
if (this.opened && this.session == null) {
this.tryOpen();
}
return this.opened && this.session != null;
}

@Override
public boolean closed() {
if (this.session == null) {
this.tryOpen();
if (!this.opened || this.session == null) {
return true;
}
return this.session == null ? true : this.session.isClosed();
return this.session.isClosed();
}

@Override
@@ -233,6 +239,7 @@ public void close() {
return;
}
this.session.close();
this.session = null;
}

@Override
@@ -123,7 +123,8 @@ public synchronized void open(HugeConfig config) {
this.store);
}

if (this.sessions.opened()) {
assert this.sessions != null;
if (!this.sessions.closed()) {
// TODO: maybe we should throw an exception here instead of ignore
LOG.debug("Store {} has been opened before", this.store);
this.sessions.useSession();
@@ -169,7 +170,7 @@ public void close() {
@Override
public boolean opened() {
this.checkClusterConnected();
return !this.sessions.session().closed();
return this.sessions.session().opened();
}

@Override
@@ -259,15 +260,16 @@ public BackendFeatures features() {
public void init() {
this.checkClusterConnected();

// Create keyspace if needed
if (!this.existsKeyspace()) {
this.initKeyspace();
}

if (this.sessions.session().opened()) {
// Session has ever been opened.
LOG.warn("Session has ever been opened(exist keyspace '{}' before)",
this.keyspace);
} else {
// Create keyspace if needed
if (!this.existsKeyspace()) {
this.initKeyspace();
}
// Open session explicitly to get the exception when it fails
this.sessions.session().open();
}
@@ -280,13 +282,16 @@ public void init() {
}

@Override
public void clear() {
public void clear(boolean clearSpace) {
this.checkClusterConnected();

if (this.existsKeyspace()) {
this.checkOpened();
this.clearTables();
this.clearKeyspace();
if (!clearSpace) {
this.checkOpened();
this.clearTables();
} else {
this.clearKeyspace();
}
}

LOG.debug("Store cleared: {}", this.store);
@@ -82,8 +82,8 @@ public void init() {
}

@Override
public void clear() {
this.store.clear();
public void clear(boolean clearSpace) {
this.store.clear(clearSpace);
}

@Override
@@ -112,7 +112,12 @@ public void init() {
public void clear() throws BackendException {
this.checkOpened();
for (BackendStore store : this.stores.values()) {
store.clear();
// Just clear tables of store, not clear space
store.clear(false);
}
for (BackendStore store : this.stores.values()) {
// Only clear space of store
store.clear(true);
}
this.notifyAndWaitEvent(Events.STORE_CLEAR);

@@ -26,12 +26,14 @@
*/
public abstract class BackendSession {

protected boolean opened;
private int refs;
private TxState txState;
private final long created;
private long updated;

public BackendSession() {
this.opened = true;
this.refs = 1;
this.txState = TxState.CLEAN;
this.created = System.currentTimeMillis();
@@ -50,10 +52,9 @@ public void update() {
this.updated = System.currentTimeMillis();
}

public abstract void open();
public abstract void close();

public abstract boolean closed();

public abstract Object commit();

public abstract void rollback();
@@ -64,6 +65,18 @@ protected void reconnectIfNeeded() {
// pass
}

protected void reset() {
// pass
}

public boolean opened() {
return this.opened;
}

public boolean closed() {
return !this.opened;
}

protected int attach() {
return ++this.refs;
}
@@ -19,6 +19,8 @@

package com.baidu.hugegraph.backend.store;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@@ -37,12 +39,14 @@ public abstract class BackendSessionPool {
private final String name;
private final ThreadLocal<BackendSession> threadLocalSession;
private final AtomicInteger sessionCount;
private final Map<Long, BackendSession> sessions;

public BackendSessionPool(HugeConfig config, String name) {
this.config = config;
this.name = name;
this.threadLocalSession = new ThreadLocal<>();
this.sessionCount = new AtomicInteger(0);
this.sessions = new ConcurrentHashMap<>();
}

public HugeConfig config() {
@@ -55,6 +59,8 @@ public final BackendSession getOrNewSession() {
session = this.newSession();
assert session != null;
this.threadLocalSession.set(session);
assert !this.sessions.containsKey(Thread.currentThread().getId());
this.sessions.put(Thread.currentThread().getId(), session);
int sessionCount = this.sessionCount.incrementAndGet();
LOG.debug("Now(after connect({})) session count is: {}",
this, sessionCount);
@@ -113,9 +119,18 @@ public Pair<Integer, Integer> closeSession() {
throw e;
}
this.threadLocalSession.remove();
assert this.sessions.containsKey(Thread.currentThread().getId());
this.sessions.remove(Thread.currentThread().getId());

return Pair.of(this.sessionCount.decrementAndGet(), ref);
}

protected void forceResetSessions() {
for (BackendSession session : this.sessions.values()) {
session.reset();
}
}

public void close() {
Pair<Integer, Integer> result = Pair.of(-1, -1);
try {
@@ -46,7 +46,7 @@ public interface BackendStore {

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

// Delete all data of database (keep table structure)
@@ -177,7 +177,7 @@ public void init() {
}

@Override
public void clear() {
public void clear(boolean clearSpace) {
for (InMemoryDBTable table : this.tables()) {
table.clear(null);
}
@@ -263,9 +263,9 @@ public long getCounter(HugeType type) {
}

@Override
public void clear() {
public void clear(boolean clearSpace) {
this.counter.reset();
super.clear();
super.clear(clearSpace);
}

@Override

0 comments on commit 1774242

Please sign in to comment.