Search in sources :

Example 51 with BackendException

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());
    }
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) TextBackendEntry(com.baidu.hugegraph.backend.serializer.TextBackendEntry) TextBackendEntry(com.baidu.hugegraph.backend.serializer.TextBackendEntry) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 52 with BackendException

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);
}
Also used : Status(com.alipay.sofa.jraft.Status) StoreCommandRequest(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreCommandRequest) StoreCommandResponse(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreCommandResponse) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 53 with BackendException

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;
}
Also used : Status(com.alipay.sofa.jraft.Status) WAIT_RPC_TIMEOUT(com.baidu.hugegraph.backend.store.raft.RaftSharedContext.WAIT_RPC_TIMEOUT) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) CommonResponse(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.CommonResponse) RaftClosure(com.baidu.hugegraph.backend.store.raft.RaftClosure) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 54 with BackendException

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();
    }
}
Also used : RateLimiter(com.google.common.util.concurrent.RateLimiter) BackendException(com.baidu.hugegraph.backend.BackendException) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 55 with BackendException

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));
    }
}
Also used : BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) BackendException(com.baidu.hugegraph.backend.BackendException) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

BackendException (com.baidu.hugegraph.backend.BackendException)79 SQLException (java.sql.SQLException)15 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)6 Status (com.alipay.sofa.jraft.Status)5 PeerId (com.alipay.sofa.jraft.entity.PeerId)5 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)5 BytesBuffer (com.baidu.hugegraph.backend.serializer.BytesBuffer)4 Path (java.nio.file.Path)4 Random (java.util.Random)4 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)4 RocksDBException (org.rocksdb.RocksDBException)4 HugeGraph (com.baidu.hugegraph.HugeGraph)3 Id (com.baidu.hugegraph.backend.id.Id)3 Query (com.baidu.hugegraph.backend.query.Query)3 HugeConfig (com.baidu.hugegraph.config.HugeConfig)3 Map (java.util.Map)3 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)2 Condition (com.baidu.hugegraph.backend.query.Condition)2 Relation (com.baidu.hugegraph.backend.query.Condition.Relation)2