use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.
the class InMemoryDBStore method mutate.
protected void mutate(BackendAction item) {
BackendEntry e = item.entry();
assert e instanceof TextBackendEntry;
TextBackendEntry entry = (TextBackendEntry) e;
InMemoryDBTable table = this.table(entry.type());
switch(item.action()) {
case INSERT:
LOG.debug("[store {}] add entry: {}", this.store, entry);
table.insert(null, entry);
break;
case DELETE:
LOG.debug("[store {}] remove id: {}", this.store, entry.id());
table.delete(null, entry);
break;
case APPEND:
LOG.debug("[store {}] append entry: {}", this.store, entry);
table.append(null, entry);
break;
case ELIMINATE:
LOG.debug("[store {}] eliminate entry: {}", this.store, entry);
table.eliminate(null, entry);
break;
default:
throw new BackendException("Unsupported mutate type: %s", item.action());
}
}
use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.
the class RpcForwarder method forwardToLeader.
public void forwardToLeader(PeerId leaderId, StoreCommand command, RaftStoreClosure future) {
E.checkNotNull(leaderId, "leader id");
E.checkState(!leaderId.equals(this.nodeId), "Invalid state: current node is the leader, there is " + "no need to forward the request");
LOG.debug("The node {} forward request to leader {}", this.nodeId, leaderId);
StoreCommandRequest.Builder builder = StoreCommandRequest.newBuilder();
builder.setType(command.type());
builder.setAction(command.action());
builder.setData(ZeroByteStringHelper.wrap(command.data()));
StoreCommandRequest request = builder.build();
RpcResponseClosure<StoreCommandResponse> responseClosure;
responseClosure = new RpcResponseClosure<StoreCommandResponse>() {
@Override
public void setResponse(StoreCommandResponse response) {
if (response.getStatus()) {
LOG.debug("StoreCommandResponse status ok");
future.complete(Status.OK(), () -> null);
} else {
LOG.debug("StoreCommandResponse status error");
Status status = new Status(RaftError.UNKNOWN, "fowared request failed");
BackendException e = new BackendException("Current node isn't leader, leader " + "is [%s], failed to forward request " + "to leader: %s", leaderId, response.getMessage());
future.failure(status, e);
}
}
@Override
public void run(Status status) {
future.run(status);
}
};
this.waitRpc(leaderId.getEndpoint(), request, responseClosure);
}
use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.
the class RpcForwarder method forwardToLeader.
public <T extends Message> RaftClosure<T> forwardToLeader(PeerId leaderId, Message request) {
E.checkNotNull(leaderId, "leader id");
E.checkState(!leaderId.equals(this.nodeId), "Invalid state: current node is the leader, there is " + "no need to forward the request");
LOG.debug("The node '{}' forward request to leader '{}'", this.nodeId, leaderId);
RaftClosure<T> future = new RaftClosure<>();
RpcResponseClosure<T> responseDone = new RpcResponseClosure<T>() {
@Override
public void setResponse(T response) {
FieldDescriptor fd = response.getDescriptorForType().findFieldByName("common");
Object object = response.getField(fd);
E.checkState(object instanceof CommonResponse, "The common field must be instance of " + "CommonResponse, actual is '%s'", object != null ? object.getClass() : null);
CommonResponse commonResponse = (CommonResponse) object;
if (commonResponse.getStatus()) {
future.complete(Status.OK(), () -> response);
} else {
Status status = new Status(RaftError.UNKNOWN, "fowared request failed");
BackendException e = new BackendException("Current node isn't leader, leader " + "is [%s], failed to forward request " + "to leader: %s", leaderId, commonResponse.getMessage());
future.failure(status, e);
}
}
@Override
public void run(Status status) {
future.run(status);
}
};
this.waitRpc(leaderId.getEndpoint(), request, responseDone);
return future;
}
use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.
the class AbstractTransaction method commit.
@Watched(prefix = "tx")
@Override
public void commit() throws BackendException {
LOG.debug("Transaction commit() [auto: {}]...", this.autoCommit);
this.checkOwnerThread();
if (this.closed) {
throw new BackendException("Transaction has been closed");
}
if (this.committing) {
// It is not allowed to recursively commit in a transaction
return;
}
if (!this.hasUpdate()) {
LOG.debug("Transaction has no data to commit({})", store());
return;
}
// Do rate limit if needed
RateLimiter rateLimiter = this.graph.writeRateLimiter();
if (rateLimiter != null) {
int size = this.mutationSize();
double time = size > 0 ? rateLimiter.acquire(size) : 0.0;
if (time > 0) {
LOG.debug("Waited for {}s to mutate {} item(s)", time, size);
}
BackendEntryIterator.checkInterrupted();
}
// Do commit
assert !this.committing : "Not allowed to commit when it's committing";
this.committing = true;
try {
this.commit2Backend();
} finally {
this.committing = false;
this.reset();
}
}
use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.
the class AbstractTransaction method commitOrRollback.
@Watched(prefix = "tx")
public void commitOrRollback() {
LOG.debug("Transaction commitOrRollback()");
this.checkOwnerThread();
/*
* The mutation will be reset after commit, in order to log the
* mutation after failure, let's save it to a local variable.
*/
BackendMutation mutation = this.mutation();
try {
// Do commit
this.commit();
} catch (Throwable e1) {
LOG.error("Failed to commit changes:", e1);
// Do rollback
try {
this.rollback();
} catch (Throwable e2) {
LOG.error("Failed to rollback changes:\n {}", mutation, e2);
}
/*
* Rethrow the commit exception
* The e.getMessage maybe too long to see key information,
*/
throw new BackendException("Failed to commit changes: %s(%s)", StringUtils.abbreviateMiddle(e1.getMessage(), ".", 256), HugeException.rootCause(e1));
}
}
Aggregations