Search in sources :

Example 26 with BackendException

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

the class PerfExample1 method testInsert.

@Override
protected void testInsert(GraphManager graph, int times, int multiple) {
    List<Object> personIds = new ArrayList<>(PERSON_NUM * multiple);
    List<Object> softwareIds = new ArrayList<>(SOFTWARE_NUM * multiple);
    for (int time = 0; time < times; time++) {
        LOG.debug("============== random person vertex ===============");
        for (int i = 0; i < PERSON_NUM * multiple; i++) {
            Random random = new Random();
            int age = random.nextInt(70);
            String name = "P" + random.nextInt();
            Vertex vertex = graph.addVertex(T.label, "person", "name", name, "age", age);
            personIds.add(vertex.id());
            LOG.debug("Add person: {}", vertex);
        }
        LOG.debug("============== random software vertex ============");
        for (int i = 0; i < SOFTWARE_NUM * multiple; i++) {
            Random random = new Random();
            int price = random.nextInt(10000) + 1;
            String name = "S" + random.nextInt();
            Vertex vertex = graph.addVertex(T.label, "software", "name", name, "lang", "java", "price", price);
            softwareIds.add(vertex.id());
            LOG.debug("Add software: {}", vertex);
        }
        LOG.debug("========== random knows & created edges ==========");
        for (int i = 0; i < EDGE_NUM / 2 * multiple; i++) {
            Random random = new Random();
            // Add edge: person --knows-> person
            Object p1 = personIds.get(random.nextInt(PERSON_NUM));
            Object p2 = personIds.get(random.nextInt(PERSON_NUM));
            graph.getVertex(p1).addEdge("knows", graph.getVertex(p2));
            // Add edge: person --created-> software
            Object p3 = personIds.get(random.nextInt(PERSON_NUM));
            Object s1 = softwareIds.get(random.nextInt(SOFTWARE_NUM));
            graph.getVertex(p3).addEdge("created", graph.getVertex(s1));
        }
        try {
            graph.tx().commit();
        } catch (BackendException e) {
            if (e.getCause() instanceof NoHostAvailableException) {
                LOG.warn("Failed to commit tx: {}", e.getMessage());
            } else {
                throw e;
            }
        }
        this.vertices.addAll(personIds);
        this.vertices.addAll(softwareIds);
        personIds.clear();
        softwareIds.clear();
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Random(java.util.Random) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) ArrayList(java.util.ArrayList) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 27 with BackendException

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

the class HbaseStore method init.

@Override
public void init() {
    this.checkConnectionOpened();
    // Create namespace
    try {
        this.sessions.createNamespace();
    } catch (NamespaceExistException ignored) {
    // Ignore due to both schema & graph store would create namespace
    } catch (IOException e) {
        throw new BackendException("Failed to create namespace '%s' for '%s' store", e, this.namespace, this.store);
    }
    // Create tables
    for (String table : this.tableNames()) {
        try {
            if (table.equals("g_oe") || table.equals("g_ie")) {
                this.sessions.createPreSplitTable(table, HbaseTable.cfs(), this.edgeLogicPartitions);
            } else if (table.equals("g_v")) {
                this.sessions.createPreSplitTable(table, HbaseTable.cfs(), this.vertexLogicPartitions);
            } else {
                this.sessions.createTable(table, HbaseTable.cfs());
            }
        } catch (TableExistsException ignored) {
            continue;
        } catch (IOException e) {
            throw new BackendException("Failed to create table '%s' for '%s' store", e, table, this.store);
        }
    }
    LOG.debug("Store initialized: {}", this.store);
}
Also used : TableExistsException(org.apache.hadoop.hbase.TableExistsException) NamespaceExistException(org.apache.hadoop.hbase.NamespaceExistException) IOException(java.io.IOException) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 28 with BackendException

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

the class HbaseStore method truncate.

@Override
public void truncate() {
    this.checkOpened();
    // Total time may cost 3 * TRUNCATE_TIMEOUT, due to there are 3 stores
    long timeout = this.sessions.config().get(HbaseOptions.TRUNCATE_TIMEOUT);
    long start = System.currentTimeMillis();
    BiFunction<String, Future<Void>, Void> wait = (table, future) -> {
        long elapsed = System.currentTimeMillis() - start;
        long remainingTime = timeout - elapsed / 1000L;
        try {
            return future.get(remainingTime, TimeUnit.SECONDS);
        } catch (Exception e) {
            throw new BackendException("Error when truncating table '%s' of '%s' store: %s", table, this.store, e.toString());
        }
    };
    // Truncate tables
    List<String> tables = this.tableNames();
    Map<String, Future<Void>> futures = new HashMap<>(tables.size());
    try {
        // Disable tables async
        for (String table : tables) {
            futures.put(table, this.sessions.disableTableAsync(table));
        }
        for (Map.Entry<String, Future<Void>> entry : futures.entrySet()) {
            wait.apply(entry.getKey(), entry.getValue());
        }
    } catch (Exception e) {
        this.enableTables();
        throw new BackendException("Failed to disable table for '%s' store", e, this.store);
    }
    try {
        // Truncate tables async
        for (String table : tables) {
            futures.put(table, this.sessions.truncateTableAsync(table));
        }
        for (Map.Entry<String, Future<Void>> entry : futures.entrySet()) {
            wait.apply(entry.getKey(), entry.getValue());
        }
    } catch (Exception e) {
        this.enableTables();
        throw new BackendException("Failed to truncate table for '%s' store", e, this.store);
    }
    LOG.debug("Store truncated: {}", this.store);
}
Also used : TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) BiFunction(java.util.function.BiFunction) BackendException(com.baidu.hugegraph.backend.BackendException) HashMap(java.util.HashMap) BackendStoreProvider(com.baidu.hugegraph.backend.store.BackendStoreProvider) ConnectionException(com.baidu.hugegraph.exception.ConnectionException) AbstractBackendStore(com.baidu.hugegraph.backend.store.AbstractBackendStore) Future(java.util.concurrent.Future) BackendAction(com.baidu.hugegraph.backend.store.BackendAction) Map(java.util.Map) Query(com.baidu.hugegraph.backend.query.Query) Session(com.baidu.hugegraph.backend.store.hbase.HbaseSessions.Session) E(com.baidu.hugegraph.util.E) NamespaceExistException(org.apache.hadoop.hbase.NamespaceExistException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) TableExistsException(org.apache.hadoop.hbase.TableExistsException) BackendFeatures(com.baidu.hugegraph.backend.store.BackendFeatures) IOException(java.io.IOException) BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Log(com.baidu.hugegraph.util.Log) Id(com.baidu.hugegraph.backend.id.Id) HugeConfig(com.baidu.hugegraph.config.HugeConfig) HugeType(com.baidu.hugegraph.type.HugeType) HashMap(java.util.HashMap) TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) BackendException(com.baidu.hugegraph.backend.BackendException) ConnectionException(com.baidu.hugegraph.exception.ConnectionException) NamespaceExistException(org.apache.hadoop.hbase.NamespaceExistException) TableExistsException(org.apache.hadoop.hbase.TableExistsException) IOException(java.io.IOException) BackendException(com.baidu.hugegraph.backend.BackendException) Future(java.util.concurrent.Future) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with BackendException

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

the class HbaseTable method newEntryIterator.

protected BackendEntryIterator newEntryIterator(Query query, RowIterator rows) {
    return new BinaryEntryIterator<>(rows, query, (entry, row) -> {
        E.checkState(!row.isEmpty(), "Can't parse empty HBase result");
        byte[] id = row.getRow();
        if (entry == null || !Bytes.prefixWith(id, entry.id().asBytes())) {
            HugeType type = query.resultType();
            // NOTE: only support BinaryBackendEntry currently
            entry = new BinaryBackendEntry(type, id, this.enablePartition);
        }
        try {
            this.parseRowColumns(row, entry, query, this.enablePartition);
        } catch (IOException e) {
            throw new BackendException("Failed to read HBase columns", e);
        }
        return entry;
    });
}
Also used : BinaryEntryIterator(com.baidu.hugegraph.backend.serializer.BinaryEntryIterator) BinaryBackendEntry(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry) IOException(java.io.IOException) HugeType(com.baidu.hugegraph.type.HugeType) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 30 with BackendException

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

the class MysqlSessions method createDatabase.

public void createDatabase() {
    // Create database with non-database-session
    LOG.debug("Create database: {}", this.database());
    String sql = this.buildCreateDatabase(this.database());
    try (Connection conn = this.openWithoutDB(0)) {
        conn.createStatement().execute(sql);
    } catch (SQLException e) {
        if (!e.getMessage().endsWith("already exists")) {
            throw new BackendException("Failed to create database '%s'", e, this.database());
        }
    // Ignore exception if database already exists
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) 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