Search in sources :

Example 61 with BackendException

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

the class PerfExample2 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++) {
            Vertex vertex = graph.addVertex(T.label, "person");
            personIds.add(vertex.id());
            LOG.debug("Add person: {}", vertex);
        }
        LOG.debug("============== random software vertex ============");
        for (int i = 0; i < SOFTWARE_NUM * multiple; i++) {
            Vertex vertex = graph.addVertex(T.label, "software");
            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 62 with BackendException

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

the class MysqlStore method init.

@Override
public void init() {
    this.checkClusterConnected();
    this.sessions.createDatabase();
    try {
        // Open a new session connected with specified database
        this.sessions.session().open();
    } catch (Exception e) {
        throw new BackendException("Failed to connect database '%s'", this.database);
    }
    this.checkOpened();
    this.initTables();
    LOG.debug("Store initialized: {}", this.store);
}
Also used : BackendException(com.baidu.hugegraph.backend.BackendException) ConnectionException(com.baidu.hugegraph.exception.ConnectionException) SQLException(java.sql.SQLException) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 63 with BackendException

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

the class MysqlStore method open.

@Override
public synchronized void open(HugeConfig config) {
    LOG.debug("Store open: {}", this.store);
    E.checkNotNull(config, "config");
    if (this.sessions != null && !this.sessions.closed()) {
        LOG.debug("Store {} has been opened before", this.store);
        this.sessions.useSession();
        return;
    }
    this.sessions = this.openSessionPool(config);
    if (this.sessions.existsDatabase()) {
        LOG.debug("Store connect with database: {}", this.database);
        try {
            this.sessions.open();
        } catch (Throwable e) {
            throw new ConnectionException("Failed to connect to MySQL", e);
        }
        try {
            this.sessions.session().open();
        } catch (Throwable e) {
            try {
                this.sessions.close();
            } catch (Throwable e2) {
                LOG.warn("Failed to close connection after an error", e2);
            }
            throw new BackendException("Failed to open database", e);
        }
    } else {
        if (this.isSchemaStore()) {
            LOG.info("Failed to open database '{}', " + "try to init database later", this.database);
        }
        this.sessions.session();
    }
    LOG.debug("Store opened: {}", this.store);
}
Also used : ConnectionException(com.baidu.hugegraph.exception.ConnectionException) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 64 with BackendException

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

the class MysqlStore method beginTx.

@Override
public void beginTx() {
    this.checkOpened();
    Session session = this.sessions.session();
    try {
        session.begin();
    } catch (SQLException e) {
        throw new BackendException("Failed to open transaction", e);
    }
}
Also used : SQLException(java.sql.SQLException) Session(com.baidu.hugegraph.backend.store.mysql.MysqlSessions.Session) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 65 with BackendException

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

the class LZ4Util method compress.

public static BytesBuffer compress(byte[] bytes, int blockSize, float bufferRatio) {
    float ratio = bufferRatio <= 0.0F ? DEFAULT_BUFFER_RATIO : bufferRatio;
    LZ4Factory factory = LZ4Factory.fastestInstance();
    LZ4Compressor compressor = factory.fastCompressor();
    int initBufferSize = Math.round(bytes.length / ratio);
    BytesBuffer buf = new BytesBuffer(initBufferSize);
    LZ4BlockOutputStream lz4Output = new LZ4BlockOutputStream(buf, blockSize, compressor);
    try {
        lz4Output.write(bytes);
        lz4Output.close();
    } catch (IOException e) {
        throw new BackendException("Failed to compress", e);
    }
    /*
         * If need to perform reading outside the method,
         * remember to call forReadWritten()
         */
    return buf;
}
Also used : LZ4Compressor(net.jpountz.lz4.LZ4Compressor) LZ4BlockOutputStream(net.jpountz.lz4.LZ4BlockOutputStream) LZ4Factory(net.jpountz.lz4.LZ4Factory) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) IOException(java.io.IOException) 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