Search in sources :

Example 11 with BackendException

use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.

the class CassandraShard method getSplits.

/**
 * Get splits of a table
 * @param splitPartitions: expected partitions count per split
 * @param splitSize: expected size(bytes) per split,
 *        splitPartitions will be ignored if splitSize is passed
 * @return a list of Shard
 */
public List<Shard> getSplits(long splitPartitions, long splitSize) {
    // Canonical ranges, split into pieces, fetch the splits in parallel
    ExecutorService executor = new ThreadPoolExecutor(0, 128, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
    List<Shard> splits = new ArrayList<>();
    try {
        List<Future<List<Shard>>> futures = new ArrayList<>();
        // Canonical ranges and nodes holding replicas
        Map<TokenRange, Set<Host>> masterRangeNodes = getRangeMap();
        for (TokenRange range : masterRangeNodes.keySet()) {
            /*
                 * For each token range, pick a live owner and ask it to
                 * compute bite-sized splits.
                 */
            futures.add(executor.submit(new SplitCallable(range, splitPartitions, splitSize)));
        }
        // Wait until we have all the results back
        for (Future<List<Shard>> future : futures) {
            try {
                splits.addAll(future.get());
            } catch (Exception e) {
                throw new BackendException("Can't get cassandra shards", e);
            }
        }
        assert splits.size() > masterRangeNodes.size();
    } finally {
        executor.shutdownNow();
    }
    Collections.shuffle(splits, new Random(System.nanoTime()));
    return splits;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Set(java.util.Set) ArrayList(java.util.ArrayList) BackendException(com.baidu.hugegraph.backend.BackendException) BackendException(com.baidu.hugegraph.backend.BackendException) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) TokenRange(com.datastax.driver.core.TokenRange) ArrayList(java.util.ArrayList) List(java.util.List) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Shard(com.baidu.hugegraph.backend.store.Shard)

Example 12 with BackendException

use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.

the class CassandraTable method query.

protected <R> Iterator<R> query(Query query, Function<Statement, ResultSet> fetcher, BiFunction<Query, ResultSet, Iterator<R>> parser) {
    ExtendableIterator<R> rs = new ExtendableIterator<>();
    if (query.limit() == 0L && !query.noLimit()) {
        LOG.debug("Return empty result(limit=0) for query {}", query);
        return rs;
    }
    List<Select> selects = this.query2Select(this.table(), query);
    try {
        for (Select select : selects) {
            ResultSet results = fetcher.apply(select);
            rs.extend(parser.apply(query, results));
        }
    } catch (DriverException e) {
        LOG.debug("Failed to query [{}], detail statement: {}", query, selects, e);
        throw new BackendException("Failed to query [%s]", e, query);
    }
    LOG.debug("Return {} for query {}", rs, query);
    return rs;
}
Also used : ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) DriverException(com.datastax.driver.core.exceptions.DriverException) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 13 with BackendException

use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.

the class SerializerFactory method register.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void register(String name, String classPath) {
    ClassLoader classLoader = SerializerFactory.class.getClassLoader();
    Class<?> clazz;
    try {
        clazz = classLoader.loadClass(classPath);
    } catch (Exception e) {
        throw new BackendException("Invalid class: '%s'", e, classPath);
    }
    // Check subclass
    if (!AbstractSerializer.class.isAssignableFrom(clazz)) {
        throw new BackendException("Class is not a subclass of class " + "AbstractSerializer: '%s'", classPath);
    }
    // Check exists
    if (serializers.containsKey(name)) {
        throw new BackendException("Exists serializer: %s(Class '%s')", name, serializers.get(name).getName());
    }
    // Register class
    serializers.put(name, (Class) clazz);
}
Also used : BackendException(com.baidu.hugegraph.backend.BackendException) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 14 with BackendException

use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.

the class SerializerFactory method serializer.

public static AbstractSerializer serializer(HugeConfig config, String name) {
    name = name.toLowerCase();
    switch(name) {
        case "binary":
            return new BinarySerializer(config);
        case "binaryscatter":
            return new BinaryScatterSerializer(config);
        case "text":
            return new TextSerializer(config);
        default:
    }
    Class<? extends AbstractSerializer> clazz = serializers.get(name);
    if (clazz == null) {
        throw new BackendException("Not exists serializer: '%s'", name);
    }
    assert AbstractSerializer.class.isAssignableFrom(clazz);
    try {
        return clazz.getConstructor(HugeConfig.class).newInstance(config);
    } catch (Exception e) {
        throw new BackendException(e);
    }
}
Also used : HugeConfig(com.baidu.hugegraph.config.HugeConfig) BackendException(com.baidu.hugegraph.backend.BackendException) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 15 with BackendException

use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.

the class RaftGroupManagerImpl method removePeer.

@Override
public String removePeer(String endpoint) {
    E.checkArgument(this.raftNode.selfIsLeader(), "Operation add_peer can only be executed on leader");
    PeerId peerId = PeerId.parsePeer(endpoint);
    RaftClosure<?> future = new RaftClosure<>();
    try {
        this.raftNode.node().removePeer(peerId, future);
        future.waitFinished();
    } catch (Throwable e) {
        throw new BackendException("Failed to remove peer '%s'", e, endpoint);
    }
    return peerId.toString();
}
Also used : PeerId(com.alipay.sofa.jraft.entity.PeerId) BackendException(com.baidu.hugegraph.backend.BackendException)

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