/** * Process the next item in the queue. */ protected void processQueue() { try { Collection<RequestMetric> batch = new ArrayList<>(this.batchSize); RequestMetric rm = queue.take(); batch.add(rm); queue.drainTo(batch, this.batchSize - 1); Builder builder = new Bulk.Builder(); for (RequestMetric metric : batch) { Index index = new Index.Builder(metric).refresh(false) .index(getIndexName()) .type("request").build(); //$NON-NLS-1$ builder.addAction(index); } BulkResult result = getClient().execute(builder.build()); if (!result.isSucceeded()) { System.err.println("Failed to add metric(s) to ES: " + result.getErrorMessage()); //$NON-NLS-1$ } } catch (Exception e) { // TODO better logging of this unlikely error System.err.println("Error adding metric to ES:"); //$NON-NLS-1$ e.printStackTrace(); return; } }
Index index = new Index.Builder(json).refresh(false).index(getIndexName()) .type("sharedStateProperty").id(id).build(); //$NON-NLS-1$ try {
/** * Process the next item in the queue. */ protected void processQueue() { try { Collection<RequestMetric> batch = new ArrayList<>(this.batchSize); RequestMetric rm = queue.take(); batch.add(rm); queue.drainTo(batch, this.batchSize - 1); Builder builder = new Bulk.Builder(); for (RequestMetric metric : batch) { Index index = new Index.Builder(metric).refresh(false) .index(getIndexName()) .type("request").build(); //$NON-NLS-1$ builder.addAction(index); } BulkResult result = getClient().execute(builder.build()); if (!result.isSucceeded()) { logger.warn("Failed to add metric(s) to ES"); //$NON-NLS-1$ } } catch (Exception e) { logger.warn("Error adding metric to ES"); //$NON-NLS-1$ return; } }
/** * @see io.apiman.gateway.engine.IRegistry#registerClient(io.apiman.gateway.engine.beans.Client, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public void registerClient(final Client client, final IAsyncResultHandler<Void> handler) { try { // Validate the client and populate the api map with apis found during validation. validateClient(client); String id = getClientId(client); Index index = new Index.Builder(client) .refresh(false) .index(getIndexName()) .setParameter(Parameters.OP_TYPE, "index") //$NON-NLS-1$ .type("client") //$NON-NLS-1$ .id(id) .build(); JestResult result = getClient().execute(index); if (!result.isSucceeded()) { throw new IOException(result.getErrorMessage()); } else { handler.handle(AsyncResultImpl.create((Void) null)); } } catch (IOException e) { handler.handle(AsyncResultImpl.create( new RegistrationException(Messages.i18n.format("ESRegistry.ErrorRegisteringClient"), e), //$NON-NLS-1$ Void.class)); } catch (RuntimeException re) { handler.handle(AsyncResultImpl.create(re, Void.class)); } }
/** * Indexes an entity. * @param type * @param id * @param sourceEntity * @param refresh true if the operation should wait for a refresh before it returns * @throws StorageException */ @SuppressWarnings("nls") private void indexEntity(String type, String id, XContentBuilder sourceEntity, boolean refresh) throws StorageException { try { String json = sourceEntity.string(); JestResult response = esClient.execute(new Index.Builder(json).refresh(refresh).index(getIndexName()) .setParameter(Parameters.OP_TYPE, "create").type(type).id(id).build()); if (!response.isSucceeded()) { throw new StorageException("Failed to index document " + id + " of type " + type + ": " + response.getErrorMessage()); } } catch (StorageException e) { throw e; } catch (Exception e) { throw new StorageException(e); } }
/** * @see io.apiman.gateway.engine.IRegistry#publishApi(io.apiman.gateway.engine.beans.Api, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public void publishApi(final Api api, final IAsyncResultHandler<Void> handler) { try { String id = getApiId(api); Index index = new Index.Builder(api).refresh(false) .index(getIndexName()).setParameter(Parameters.OP_TYPE, "index") //$NON-NLS-1$ .type("api").id(id).build(); //$NON-NLS-1$ JestResult result = getClient().execute(index); if (!result.isSucceeded()) { throw new IOException(result.getErrorMessage()); } else { handler.handle(AsyncResultImpl.create((Void) null)); } } catch (Exception e) { handler.handle(AsyncResultImpl.create( new PublishingException(Messages.i18n.format("ESRegistry.ErrorPublishingApi"), e), //$NON-NLS-1$ Void.class)); } }
/** * Indexes an entity. * @param type * @param id * @param sourceEntity * @param refresh true if the operation should wait for a refresh before it returns * @throws StorageException */ @SuppressWarnings("nls") private void indexEntity(String type, String id, XContentBuilder sourceEntity, boolean refresh) throws StorageException { try { String json = sourceEntity.string(); JestResult response = esClient.execute(new Index.Builder(json).refresh(refresh).index(getIndexName()) .setParameter(Parameters.OP_TYPE, "create").type(type).id(id).build()); if (!response.isSucceeded()) { throw new StorageException("Failed to index document " + id + " of type " + type + ": " + response.getErrorMessage()); } } catch (StorageException e) { throw e; } catch (Exception e) { throw new StorageException(e); } }
/** * @see io.apiman.gateway.engine.IRegistry#registerClient(io.apiman.gateway.engine.beans.Client, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public void registerClient(final Client client, final IAsyncResultHandler<Void> handler) { try { // Validate the client and populate the api map with apis found during validation. validateClient(client); String id = getClientId(client); Index index = new Index.Builder(client) .refresh(false) .index(getIndexName()) .setParameter(Parameters.OP_TYPE, "index") //$NON-NLS-1$ .type("client") //$NON-NLS-1$ .id(id) .build(); JestResult result = getClient().execute(index); if (!result.isSucceeded()) { throw new IOException(result.getErrorMessage()); } else { handler.handle(AsyncResultImpl.create((Void) null)); } } catch (IOException e) { handler.handle(AsyncResultImpl.create( new RegistrationException(Messages.i18n.format("ESRegistry.ErrorRegisteringClient"), e), //$NON-NLS-1$ Void.class)); } catch (RuntimeException re) { handler.handle(AsyncResultImpl.create(re, Void.class)); } }
/** * Stores a "dataversion" record in the ES store. There is only a single one of these. The * return value of the add will include the version number of the entity. This version * number is what we use to determine whether our cache is stale. */ protected void updateDataVersion() { DataVersionBean dv = new DataVersionBean(); dv.setUpdatedOn(System.currentTimeMillis()); Index index = new Index.Builder(dv).refresh(false) .index(getDefaultIndexName()) .type("dataVersion").id("instance").build(); //$NON-NLS-1$ //$NON-NLS-2$ getClient().executeAsync(index, new JestResultHandler<JestResult>() { @Override public void completed(JestResult result) { dataVersion = null; } @Override public void failed(Exception e) { dataVersion = null; } }); }
/** * @see io.apiman.gateway.engine.IRegistry#publishApi(io.apiman.gateway.engine.beans.Api, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public void publishApi(final Api api, final IAsyncResultHandler<Void> handler) { try { String id = getApiId(api); Index index = new Index.Builder(api).refresh(false) .index(getIndexName()).setParameter(Parameters.OP_TYPE, "index") //$NON-NLS-1$ .type("api").id(id).build(); //$NON-NLS-1$ JestResult result = getClient().execute(index); if (!result.isSucceeded()) { throw new IOException(result.getErrorMessage()); } else { handler.handle(AsyncResultImpl.create((Void) null)); } } catch (Exception e) { handler.handle(AsyncResultImpl.create( new PublishingException(Messages.i18n.format("ESRegistry.ErrorPublishingApi"), e), //$NON-NLS-1$ Void.class)); } }
/** * Stores a "dataversion" record in the ES store. There is only a single one of these. The * return value of the add will include the version number of the entity. This version * number is what we use to determine whether our cache is stale. */ protected void updateDataVersion() { DataVersionBean dv = new DataVersionBean(); dv.setUpdatedOn(System.currentTimeMillis()); Index index = new Index.Builder(dv).refresh(false) .index(getDefaultIndexName()) .type("dataVersion").id("instance").build(); //$NON-NLS-1$ //$NON-NLS-2$ getClient().executeAsync(index, new JestResultHandler<JestResult>() { @Override public void completed(JestResult result) { dataVersion = null; } @Override public void failed(Exception e) { dataVersion = null; } }); }
/** * @see io.apiman.gateway.engine.components.ICacheStoreComponent#put(java.lang.String, java.lang.Object, long) */ @Override public <T> void put(String cacheKey, T jsonObject, long timeToLive) throws IOException { ESCacheEntry entry = new ESCacheEntry(); entry.setData(null); entry.setExpiresOn(System.currentTimeMillis() + (timeToLive * 1000)); entry.setHead(mapper.writeValueAsString(jsonObject)); Index index = new Index.Builder(entry).refresh(false).index(getIndexName()) .type("cacheEntry").id(cacheKey).build(); //$NON-NLS-1$ try { getClient().execute(index); } catch (Throwable e) { } }
/** * @see io.apiman.gateway.engine.components.ICacheStoreComponent#put(java.lang.String, java.lang.Object, long) */ @Override public <T> void put(String cacheKey, T jsonObject, long timeToLive) throws IOException { ESCacheEntry entry = new ESCacheEntry(); entry.setData(null); entry.setExpiresOn(System.currentTimeMillis() + (timeToLive * 1000)); entry.setHead(mapper.writeValueAsString(jsonObject)); Index index = new Index.Builder(entry).refresh(false).index(getIndexName()) .type("cacheEntry").id(cacheKey).build(); //$NON-NLS-1$ try { getClient().execute(index); } catch (Throwable e) { } }
@Override public void end() { if (!aborted) { entry.setData(Base64.encodeBase64String(data.getBytes())); Index index = new Index.Builder(entry).refresh(false).index(getIndexName()) .type("cacheEntry").id(cacheKey).build(); //$NON-NLS-1$ try { getClient().execute(index); } catch (Throwable e) { } } finished = true; } };
@Override public void end() { if (!aborted) { entry.setData(Base64.encodeBase64String(data.getBytes())); Index index = new Index.Builder(entry).refresh(false).index(getIndexName()) .type("cacheEntry").id(cacheKey).build(); //$NON-NLS-1$ try { getClient().execute(index); } catch (Throwable e) { } } finished = true; } };
public Index queryCreateEvent(Event event) { return new Index.Builder(event).index(connection.getIndexName()).type(ElasticConstants.TYPE_EVENT).refresh(true) .build(); }
public Index queryCreateProperty(Property<?> property) { return new Index.Builder(property).index(connection.getIndexName()).type(ElasticConstants.TYPE_PROPERTY) .refresh(true).build(); }
public Index queryCreateFeature(Feature fp) { return new Index.Builder(fp).index(connection.getIndexName()).type(ElasticConstants.TYPE_FEATURE).refresh(true) .build(); }
public Index queryUpdateFeature(Feature fp) { return new Index.Builder(fp).index(connection.getIndexName()).type(ElasticConstants.TYPE_FEATURE) .id(getFeatureTechId(fp.getUid())).refresh(true).build(); }
private Action<?> indexDocument(String id, Document document, Class<?> entityType, boolean refresh) { JsonObject source = convertToJson( document, entityType ); String type = entityType.getName(); Index index = new Index.Builder( source ) .index( indexName ) .type( type ) .id( id ) .refresh( refresh ) .build(); return index; }