Search in sources :

Example 6 with BackendException

use of org.janusgraph.diskstorage.BackendException in project janusgraph by JanusGraph.

the class ElasticSearchIndex method pushMapping.

/**
 * Push mapping to ElasticSearch
 * @param store the type in the index
 * @param key the name of the property in the index
 * @param information information of the key
 */
private void pushMapping(String store, String key, KeyInformation information) throws AssertionError, PermanentBackendException, BackendException {
    final Class<?> dataType = information.getDataType();
    Mapping map = Mapping.getMapping(information);
    final Map<String, Object> properties = new HashMap<>();
    if (AttributeUtil.isString(dataType)) {
        if (map == Mapping.DEFAULT)
            map = Mapping.TEXT;
        log.debug("Registering string type for {} with mapping {}", key, map);
        final String stringAnalyzer = ParameterType.STRING_ANALYZER.findParameter(information.getParameters(), null);
        final String textAnalyzer = ParameterType.TEXT_ANALYZER.findParameter(information.getParameters(), null);
        // use keyword type for string mappings unless custom string analyzer is provided
        final Map<String, Object> stringMapping = stringAnalyzer == null ? compat.createKeywordMapping() : compat.createTextMapping(stringAnalyzer);
        switch(map) {
            case STRING:
                properties.put(key, stringMapping);
                break;
            case TEXT:
                properties.put(key, compat.createTextMapping(textAnalyzer));
                break;
            case TEXTSTRING:
                properties.put(key, compat.createTextMapping(textAnalyzer));
                properties.put(getDualMappingName(key), stringMapping);
                break;
            default:
                throw new AssertionError("Unexpected mapping: " + map);
        }
    } else if (dataType == Float.class) {
        log.debug("Registering float type for {}", key);
        properties.put(key, ImmutableMap.of(ES_TYPE_KEY, "float"));
    } else if (dataType == Double.class) {
        log.debug("Registering double type for {}", key);
        properties.put(key, ImmutableMap.of(ES_TYPE_KEY, "double"));
    } else if (dataType == Byte.class) {
        log.debug("Registering byte type for {}", key);
        properties.put(key, ImmutableMap.of(ES_TYPE_KEY, "byte"));
    } else if (dataType == Short.class) {
        log.debug("Registering short type for {}", key);
        properties.put(key, ImmutableMap.of(ES_TYPE_KEY, "short"));
    } else if (dataType == Integer.class) {
        log.debug("Registering integer type for {}", key);
        properties.put(key, ImmutableMap.of(ES_TYPE_KEY, "integer"));
    } else if (dataType == Long.class) {
        log.debug("Registering long type for {}", key);
        properties.put(key, ImmutableMap.of(ES_TYPE_KEY, "long"));
    } else if (dataType == Boolean.class) {
        log.debug("Registering boolean type for {}", key);
        properties.put(key, ImmutableMap.of(ES_TYPE_KEY, "boolean"));
    } else if (dataType == Geoshape.class) {
        switch(map) {
            case PREFIX_TREE:
                final int maxLevels = ParameterType.INDEX_GEO_MAX_LEVELS.findParameter(information.getParameters(), DEFAULT_GEO_MAX_LEVELS);
                final double distErrorPct = ParameterType.INDEX_GEO_DIST_ERROR_PCT.findParameter(information.getParameters(), DEFAULT_GEO_DIST_ERROR_PCT);
                log.debug("Registering geo_shape type for {} with tree_levels={} and distance_error_pct={}", key, maxLevels, distErrorPct);
                properties.put(key, ImmutableMap.of(ES_TYPE_KEY, "geo_shape", "tree", "quadtree", "tree_levels", maxLevels, "distance_error_pct", distErrorPct));
                break;
            default:
                log.debug("Registering geo_point type for {}", key);
                properties.put(key, ImmutableMap.of(ES_TYPE_KEY, "geo_point"));
        }
    } else if (dataType == Date.class || dataType == Instant.class) {
        log.debug("Registering date type for {}", key);
        properties.put(key, ImmutableMap.of(ES_TYPE_KEY, "date"));
    } else if (dataType == UUID.class) {
        log.debug("Registering uuid type for {}", key);
        properties.put(key, compat.createKeywordMapping());
    }
    if (useAllField && client.getMajorVersion().getValue() >= 6) {
        // add custom all field mapping if it doesn't exist
        properties.put(ElasticSearchConstants.CUSTOM_ALL_FIELD, compat.createTextMapping(null));
        // add copy_to for custom all field mapping
        if (properties.containsKey(key) && dataType != Geoshape.class) {
            final Map<String, Object> mapping = new HashMap<>(((Map<String, Object>) properties.get(key)));
            mapping.put("copy_to", ElasticSearchConstants.CUSTOM_ALL_FIELD);
            properties.put(key, mapping);
        }
    }
    final Map<String, Object> mapping = ImmutableMap.of("properties", properties);
    try {
        client.createMapping(getIndexStoreName(store), store, mapping);
    } catch (final Exception e) {
        throw convert(e);
    }
}
Also used : HashMap(java.util.HashMap) Geoshape(org.janusgraph.core.attribute.Geoshape) Mapping(org.janusgraph.core.schema.Mapping) IndexMapping(org.janusgraph.diskstorage.es.IndexMappings.IndexMapping) UncheckedIOException(java.io.UncheckedIOException) TemporaryBackendException(org.janusgraph.diskstorage.TemporaryBackendException) JanusGraphException(org.janusgraph.core.JanusGraphException) BackendException(org.janusgraph.diskstorage.BackendException) IOException(java.io.IOException) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 7 with BackendException

use of org.janusgraph.diskstorage.BackendException in project janusgraph by JanusGraph.

the class ElasticSearchConfigTest method testExternalMappingsViaTemplate.

@Test
public void testExternalMappingsViaTemplate() throws Exception {
    final Duration maxWrite = Duration.ofMillis(2000L);
    final String storeName = "test_mapping";
    final Configuration indexConfig = GraphDatabaseConfiguration.buildGraphConfiguration().set(USE_EXTERNAL_MAPPINGS, true, INDEX_NAME).restrictTo(INDEX_NAME);
    final IndexProvider idx = open(indexConfig);
    final ElasticMajorVersion version = ((ElasticSearchIndex) idx).getVersion();
    final HttpPut newTemplate = new HttpPut("_template/template_1");
    final Map<String, Object> content = ImmutableMap.of("template", "janusgraph_test_mapping*", "mappings", readMapping(version, "/strict_mapping.json").getMappings());
    newTemplate.setEntity(new StringEntity(objectMapper.writeValueAsString(content), Charset.forName("UTF-8")));
    executeRequest(newTemplate);
    final HttpPut newMapping = new HttpPut("janusgraph_" + storeName);
    executeRequest(newMapping);
    final KeyInformation.IndexRetriever indexRetriever = IndexProviderTest.getIndexRetriever(IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD));
    final BaseTransactionConfig txConfig = StandardBaseTransactionConfig.of(TimestampProviders.MILLI);
    final IndexTransaction itx = new IndexTransaction(idx, indexRetriever, txConfig, maxWrite);
    // Test date property OK
    idx.register(storeName, "date", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("date"), itx);
    // Test weight property KO
    try {
        idx.register(storeName, "weight", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("weight"), itx);
        fail("should fail");
    } catch (final BackendException ignored) {
    }
    itx.rollback();
    idx.close();
}
Also used : BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) Configuration(org.janusgraph.diskstorage.configuration.Configuration) CommonsConfiguration(org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration) GraphDatabaseConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration) BasicConfiguration(org.janusgraph.diskstorage.configuration.BasicConfiguration) ModifiableConfiguration(org.janusgraph.diskstorage.configuration.ModifiableConfiguration) Duration(java.time.Duration) StandardBaseTransactionConfig(org.janusgraph.diskstorage.util.StandardBaseTransactionConfig) BaseTransactionConfig(org.janusgraph.diskstorage.BaseTransactionConfig) HttpPut(org.apache.http.client.methods.HttpPut) BackendException(org.janusgraph.diskstorage.BackendException) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) StringEntity(org.apache.http.entity.StringEntity) ElasticSearchIndex(org.janusgraph.diskstorage.es.ElasticSearchIndex) Test(org.junit.Test)

Example 8 with BackendException

use of org.janusgraph.diskstorage.BackendException in project janusgraph by JanusGraph.

the class MetricInstrumentedStore method runWithMetrics.

static <T> T runWithMetrics(StoreTransaction txh, String storeName, String name, StorageCallable<T> impl) throws BackendException {
    if (!txh.getConfiguration().hasGroupName()) {
        return impl.call();
    }
    String prefix = txh.getConfiguration().getGroupName();
    Preconditions.checkNotNull(name);
    Preconditions.checkNotNull(impl);
    final MetricManager mgr = MetricManager.INSTANCE;
    mgr.getCounter(prefix, storeName, name, M_CALLS).inc();
    final Timer.Context tc = mgr.getTimer(prefix, storeName, name, M_TIME).time();
    try {
        return impl.call();
    } catch (BackendException | RuntimeException e) {
        mgr.getCounter(prefix, storeName, name, M_EXCEPTIONS).inc();
        throw e;
    } finally {
        tc.stop();
    }
}
Also used : MetricManager(org.janusgraph.util.stats.MetricManager) Timer(com.codahale.metrics.Timer) BackendException(org.janusgraph.diskstorage.BackendException)

Example 9 with BackendException

use of org.janusgraph.diskstorage.BackendException in project janusgraph by JanusGraph.

the class BackendOperation method executeDirect.

public static <V> V executeDirect(Callable<V> exe, Duration totalWaitTime) throws BackendException {
    Preconditions.checkArgument(!totalWaitTime.isZero(), "Need to specify a positive waitTime: %s", totalWaitTime);
    long maxTime = System.currentTimeMillis() + totalWaitTime.toMillis();
    Duration waitTime = pertubTime(BASE_REATTEMPT_TIME);
    BackendException lastException;
    while (true) {
        try {
            return exe.call();
        } catch (final Throwable e) {
            // Find inner-most StorageException
            Throwable ex = e;
            BackendException storeEx = null;
            do {
                if (ex instanceof BackendException)
                    storeEx = (BackendException) ex;
            } while ((ex = ex.getCause()) != null);
            if (storeEx != null && storeEx instanceof TemporaryBackendException) {
                lastException = storeEx;
            } else if (e instanceof BackendException) {
                throw (BackendException) e;
            } else {
                throw new PermanentBackendException("Permanent exception while executing backend operation " + exe.toString(), e);
            }
        }
        // Wait and retry
        assert lastException != null;
        if (System.currentTimeMillis() + waitTime.toMillis() < maxTime) {
            log.info("Temporary exception during backend operation [" + exe.toString() + "]. Attempting backoff retry.", lastException);
            try {
                Thread.sleep(waitTime.toMillis());
            } catch (InterruptedException r) {
                // added thread interrupt signal to support traversal interruption
                Thread.currentThread().interrupt();
                throw new PermanentBackendException("Interrupted while waiting to retry failed backend operation", r);
            }
        } else {
            break;
        }
        waitTime = pertubTime(waitTime.multipliedBy(2));
    }
    throw new TemporaryBackendException("Could not successfully complete backend operation due to repeated temporary exceptions after " + totalWaitTime, lastException);
}
Also used : TemporaryBackendException(org.janusgraph.diskstorage.TemporaryBackendException) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) Duration(java.time.Duration) BackendException(org.janusgraph.diskstorage.BackendException) TemporaryBackendException(org.janusgraph.diskstorage.TemporaryBackendException) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException)

Example 10 with BackendException

use of org.janusgraph.diskstorage.BackendException in project janusgraph by JanusGraph.

the class MetricInstrumentedStoreManager method mutateMany.

@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> mutations, StoreTransaction txh) throws BackendException {
    if (!txh.getConfiguration().hasGroupName()) {
        backend.mutateMany(mutations, txh);
    }
    String prefix = txh.getConfiguration().getGroupName();
    final MetricManager mgr = MetricManager.INSTANCE;
    mgr.getCounter(prefix, managerMetricsName, M_MUTATE, M_CALLS).inc();
    final Timer.Context tc = mgr.getTimer(prefix, managerMetricsName, M_MUTATE, M_TIME).time();
    try {
        backend.mutateMany(mutations, txh);
    } catch (BackendException | RuntimeException e) {
        mgr.getCounter(prefix, managerMetricsName, M_MUTATE, M_EXCEPTIONS).inc();
        throw e;
    } finally {
        tc.stop();
    }
}
Also used : MetricManager(org.janusgraph.util.stats.MetricManager) Timer(com.codahale.metrics.Timer) BackendException(org.janusgraph.diskstorage.BackendException)

Aggregations

BackendException (org.janusgraph.diskstorage.BackendException)32 PermanentBackendException (org.janusgraph.diskstorage.PermanentBackendException)12 ArrayList (java.util.ArrayList)9 TemporaryBackendException (org.janusgraph.diskstorage.TemporaryBackendException)9 IOException (java.io.IOException)7 List (java.util.List)7 Map (java.util.Map)7 TException (org.apache.thrift.TException)7 JanusGraphException (org.janusgraph.core.JanusGraphException)7 CTConnection (org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection)7 UncheckedIOException (java.io.UncheckedIOException)6 HashMap (java.util.HashMap)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 Duration (java.time.Duration)4 BaseTransactionConfig (org.janusgraph.diskstorage.BaseTransactionConfig)4 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)4 BackendOperation (org.janusgraph.diskstorage.util.BackendOperation)4 GraphDatabaseConfiguration (org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration)4 SolrServerException (org.apache.solr.client.solrj.SolrServerException)3 KeeperException (org.apache.zookeeper.KeeperException)3