Skip to content

Commit

Permalink
hugegraph-1379 support restore graph in restoring mode and merging mode
Browse files Browse the repository at this point in the history
implement: #182

Change-Id: I7c2c85435af01c5d27279076d0473685b8f23f34
  • Loading branch information
zhoney authored and Linary committed Nov 21, 2018
1 parent 2c356c8 commit 255df7e
Show file tree
Hide file tree
Showing 41 changed files with 1,367 additions and 316 deletions.
2 changes: 1 addition & 1 deletion hugegraph-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Implementation-Version>0.30.0.0</Implementation-Version>
<Implementation-Version>0.31.0.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
36 changes: 16 additions & 20 deletions hugegraph-api/src/main/java/com/baidu/hugegraph/api/GraphsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.baidu.hugegraph.api;

import java.io.File;
import java.util.Map;
import java.util.Set;

import javax.annotation.security.RolesAllowed;
Expand All @@ -44,9 +45,10 @@
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.schema.SchemaManager;
import com.baidu.hugegraph.server.RestServer;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

Expand Down Expand Up @@ -152,38 +154,32 @@ public void clear(@Context GraphManager manager,

@PUT
@Timed
@Path("{name}/restoring")
@Path("{name}/mode")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed("admin")
public Object restoring(@Context GraphManager manager,
@PathParam("name") String name,
JsonRestoring jsonRestoring) {
LOG.debug("Set restoring status to: '{}' of graph '{}'",
jsonRestoring, name);
public Map<String, GraphMode> mode(@Context GraphManager manager,
@PathParam("name") String name,
GraphMode mode) {
LOG.debug("Set mode to: '{}' of graph '{}'", mode, name);

E.checkArgument(mode != null, "Graph mode can't be null");
HugeGraph g = graph(manager, name);
g.restoring(jsonRestoring.restoring);
return jsonRestoring;
g.mode(mode);
return ImmutableMap.of("mode", mode);
}

@GET
@Timed
@Path("{name}/restoring")
@Path("{name}/mode")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed("admin")
public Object restoring(@Context GraphManager manager,
@PathParam("name") String name) {
LOG.debug("Get restoring status of graph '{}'", name);
public Map<String, GraphMode> mode(@Context GraphManager manager,
@PathParam("name") String name) {
LOG.debug("Get mode of graph '{}'", name);

HugeGraph g = graph(manager, name);
return ImmutableMap.of("restoring", g.restoring());
}

private static class JsonRestoring {

@JsonProperty("restoring")
public boolean restoring;
return ImmutableMap.of("mode", g.mode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.schema.EdgeLabel;
import com.baidu.hugegraph.type.define.Frequency;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
import com.codahale.metrics.annotation.Timed;
Expand Down Expand Up @@ -178,6 +179,16 @@ public void checkCreate(boolean isBatch) {

private EdgeLabel.Builder convert2Builder(HugeGraph g) {
EdgeLabel.Builder builder = g.schema().edgeLabel(this.name);
if (this.id != 0) {
E.checkArgument(this.id > 0,
"Only positive number can be assign as " +
"edge label id");
E.checkArgument(g.mode() == GraphMode.RESTORING,
"Only accept edge label id when graph in " +
"RESTORING mode, but '%s' is in mode '%s'",
g, g.mode());
builder.id(this.id);
}
if (this.sourceLabel != null) {
builder.sourceLabel(this.sourceLabel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.schema.IndexLabel;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.type.define.IndexType;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
Expand Down Expand Up @@ -153,6 +154,16 @@ public void checkCreate(boolean isBatch) {

private IndexLabel.Builder convert2Builder(HugeGraph g) {
IndexLabel.Builder builder = g.schema().indexLabel(this.name);
if (this.id != 0) {
E.checkArgument(this.id > 0,
"Only positive number can be assign as " +
"index label id");
E.checkArgument(g.mode() == GraphMode.RESTORING,
"Only accept index label id when graph in " +
"RESTORING mode, but '%s' is in mode '%s'",
g, g.mode());
builder.id(this.id);
}
builder.on(this.baseType, this.baseValue);
if (this.indexType != null) {
builder.indexType(this.indexType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.baidu.hugegraph.schema.PropertyKey;
import com.baidu.hugegraph.type.define.Cardinality;
import com.baidu.hugegraph.type.define.DataType;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
import com.codahale.metrics.annotation.Timed;
Expand Down Expand Up @@ -173,6 +174,16 @@ public void checkCreate(boolean isBatch) {

private PropertyKey.Builder convert2Builder(HugeGraph g) {
PropertyKey.Builder builder = g.schema().propertyKey(this.name);
if (this.id != 0) {
E.checkArgument(this.id > 0,
"Only positive number can be assign as " +
"property key id");
E.checkArgument(g.mode() == GraphMode.RESTORING,
"Only accept property key id when graph in " +
"RESTORING mode, but '%s' is in mode '%s'",
g, g.mode());
builder.id(this.id);
}
if (this.cardinality != null) {
builder.cardinality(this.cardinality);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.schema.VertexLabel;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.type.define.IdStrategy;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
Expand Down Expand Up @@ -177,6 +178,16 @@ public void checkCreate(boolean isBatch) {

private VertexLabel.Builder convert2Builder(HugeGraph g) {
VertexLabel.Builder builder = g.schema().vertexLabel(this.name);
if (this.id != 0) {
E.checkArgument(this.id > 0,
"Only positive number can be assign as " +
"vertex label id");
E.checkArgument(g.mode() == GraphMode.RESTORING,
"Only accept vertex label id when graph in " +
"RESTORING mode, but '%s' is in mode '%s'",
g, g.mode());
builder.id(this.id);
}
if (this.idStrategy != null) {
builder.idStrategy(this.idStrategy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,6 @@ public void clearBackend() {
this.hugegraph.clearBackend();
}

@Override
public void restoring(boolean restoring) {
this.verifyPermission(ROLE_ADMIN);
this.hugegraph.restoring(restoring);
}

@Override
public boolean restoring() {
this.verifyPermission(ROLE_ADMIN);
return this.hugegraph.restoring();
}

private void verifyPermission() {
/*
* The owner role should match the graph name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ public final class ApiVersion {
* [0.28] Issue-153: Add task-cancel API
* [0.29] Issue-39: Add rays and rings RESTful API
* [0.30] Issue-32: Change index create API to return indexLabel and task id
* [0.31] Issue-182: Support restore graph in restoring and merging mode
*/

// The second parameter of Version.of() is for IDE running without JAR
public static final Version VERSION = Version.of(ApiVersion.class, "0.30");
public static final Version VERSION = Version.of(ApiVersion.class, "0.31");

public static final void check() {
// Check version of hugegraph-core. Firstly do check from version 0.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,17 @@ protected void clearTables() {
}

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

@Override
public long getCounter(HugeType type) {
this.checkSessionConnected();
CassandraSessionPool.Session session = super.sessions.session();
return this.counters.getCounter(session, type);
}
}

Expand Down Expand Up @@ -522,5 +529,17 @@ public Id nextId(HugeType type) {
throw new UnsupportedOperationException(
"CassandraGraphStore.nextId()");
}

@Override
public void increaseCounter(HugeType type, long num) {
throw new UnsupportedOperationException(
"CassandraGraphStore.increaseCounter()");
}

@Override
public long getCounter(HugeType type) {
throw new UnsupportedOperationException(
"CassandraGraphStore.getCounter()");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ public static class Counters extends CassandraTable {

public static final String TABLE = "counters";

public static final int MAX_TIMES = 1000;

public Counters() {
super(TABLE);
}
Expand All @@ -84,40 +82,26 @@ public void init(CassandraSessionPool.Session session) {
this.createTable(session, pkeys, ckeys, columns);
}

public synchronized Id nextId(CassandraSessionPool.Session session,
HugeType type) {
public long getCounter(CassandraSessionPool.Session session,
HugeType type) {
Clause where = formatEQ(HugeKeys.SCHEMA_TYPE, type.name());
Select select = QueryBuilder.select(formatKey(HugeKeys.ID))
.from(TABLE);
select.where(where);

Update update = QueryBuilder.update(TABLE);
// Id increase 1
update.with(QueryBuilder.incr(formatKey(HugeKeys.ID), 1L));
update.where(where);

// Do get-increase-get-compare operation
long counter = 0L;
long expect = -1L;

for (int i = 0; i < MAX_TIMES; i++) {
Row row = session.execute(select).one();
if (row != null) {
counter = row.getLong(formatKey(HugeKeys.ID));
}
if (counter == expect) {
break;
}
// Increase local counter
expect = counter + 1L;
// Increase remote counter
session.execute(update);
Row row = session.execute(select).one();
if (row == null) {
return 0L;
} else {
return row.getLong(formatKey(HugeKeys.ID));
}
}

E.checkState(counter != 0L, "Please check whether Cassandra is OK");
E.checkState(counter == expect,
"Cassandra is busy please try again");
return IdGenerator.of(expect);
public void increaseCounter(CassandraSessionPool.Session session,
HugeType type, long increment) {
Update update = QueryBuilder.update(TABLE);
update.with(QueryBuilder.incr(formatKey(HugeKeys.ID), increment));
update.where(formatEQ(HugeKeys.SCHEMA_TYPE, type.name()));
session.execute(update);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,4 @@ public interface GremlinGraph extends Graph {
public String backend();
public void initBackend();
public void clearBackend();

public void restoring(boolean restoring);
public boolean restoring();
}
16 changes: 8 additions & 8 deletions hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@
import com.baidu.hugegraph.task.TaskScheduler;
import com.baidu.hugegraph.traversal.optimize.HugeGraphStepStrategy;
import com.baidu.hugegraph.traversal.optimize.HugeVertexStepStrategy;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.LockUtil;
import com.baidu.hugegraph.util.Log;
import com.baidu.hugegraph.variables.HugeVariables;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.RateLimiter;

/**
Expand Down Expand Up @@ -97,7 +99,7 @@ public class HugeGraph implements GremlinGraph {

private final String name;
private boolean closed;
private boolean restoring;
private GraphMode mode;

private final HugeConfig configuration;

Expand Down Expand Up @@ -128,7 +130,7 @@ public HugeGraph(HugeConfig configuration) {

this.name = configuration.get(CoreOptions.STORE);
this.closed = false;
this.restoring = false;
this.mode = GraphMode.NONE;

try {
this.storeProvider = this.loadStoreProvider();
Expand Down Expand Up @@ -163,14 +165,12 @@ public boolean closed() {
return this.closed && this.tx.closed();
}

@Override
public boolean restoring() {
return this.restoring;
public GraphMode mode() {
return this.mode;
}

@Override
public void restoring(boolean restoring) {
this.restoring = restoring;
public void mode(GraphMode mode) {
this.mode = mode;
}

public EventHub schemaEventHub() {
Expand Down
Loading

0 comments on commit 255df7e

Please sign in to comment.