Skip to content

Commit

Permalink
allow changing the ES index names
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Feb 26, 2016
1 parent 091738e commit b1d609b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 27 deletions.
Expand Up @@ -53,19 +53,23 @@ public static void clearClientCache() {
/**
* Creates a client from information in the config map.
* @param config the configuration
* @param indexName the index to use
* @param defaultIndexName the default index to use if not specified in the config
* @return the ES client
*/
public static JestClient createClient(Map<String, String> config, String indexName) {
public static JestClient createClient(Map<String, String> config, String defaultIndexName) {
JestClient client;
String clientType = config.get("client.type"); //$NON-NLS-1$
if (clientType == null) {
clientType = "jest"; //$NON-NLS-1$
}
String indexName = config.get("client.index"); //$NON-NLS-1$
if (indexName == null) {
indexName = defaultIndexName;
}
if ("jest".equals(clientType)) { //$NON-NLS-1$
client = createJestClient(config, indexName);
client = createJestClient(config, indexName, defaultIndexName);
} else if ("local".equals(clientType)) { //$NON-NLS-1$
client = createLocalClient(config, indexName);
client = createLocalClient(config, indexName, defaultIndexName);
} else {
throw new RuntimeException("Invalid elasticsearch client type: " + clientType); //$NON-NLS-1$
}
Expand All @@ -76,9 +80,10 @@ public static JestClient createClient(Map<String, String> config, String indexNa
* Creates a transport client from a configuration map.
* @param config the configuration
* @param indexName the name of the index
* @param defaultIndexName the default index name
* @return the ES client
*/
public static JestClient createJestClient(Map<String, String> config, String indexName) {
public static JestClient createJestClient(Map<String, String> config, String indexName, String defaultIndexName) {
String host = config.get("client.host"); //$NON-NLS-1$
String port = config.get("client.port"); //$NON-NLS-1$
String protocol = config.get("client.protocol"); //$NON-NLS-1$
Expand All @@ -104,7 +109,7 @@ public static JestClient createJestClient(Map<String, String> config, String ind
timeout = "6000"; //$NON-NLS-1$
}
return createJestClient(protocol, host, Integer.parseInt(port), indexName, username, password,
BooleanUtils.toBoolean(initialize), Integer.parseInt(timeout));
BooleanUtils.toBoolean(initialize), Integer.parseInt(timeout), defaultIndexName);
}

/**
Expand All @@ -117,10 +122,11 @@ public static JestClient createJestClient(Map<String, String> config, String ind
* @param password the password to authenticate with
* @param initialize true if the index should be initialized
* @param timeout the connection and read timeouts in ms
* @param defaultIndexName the default index name
* @return the ES client
*/
public static JestClient createJestClient(String protocol, String host, int port, String indexName,
String username, String password, boolean initialize, int timeout) {
String username, String password, boolean initialize, int timeout, String defaultIndexName) {
String clientKey = "jest:" + host + ':' + port + '/' + indexName; //$NON-NLS-1$
synchronized (clients) {
if (clients.containsKey(clientKey)) {
Expand Down Expand Up @@ -148,7 +154,7 @@ public static JestClient createJestClient(String protocol, String host, int port
JestClient client = factory.getObject();
clients.put(clientKey, client);
if (initialize) {
initializeClient(client, indexName);
initializeClient(client, indexName, defaultIndexName);
}
return client;
}
Expand All @@ -159,12 +165,13 @@ public static JestClient createJestClient(String protocol, String host, int port
* Creates a local client from a configuration map.
* @param config the config from apiman.properties
* @param indexName the name of the ES index
* @param defaultIndexName the default ES index name
* @return the ES client
*/
public static JestClient createLocalClient(Map<String, String> config, String indexName) {
public static JestClient createLocalClient(Map<String, String> config, String indexName, String defaultIndexName) {
String clientLocClassName = config.get("client.class"); //$NON-NLS-1$
String clientLocFieldName = config.get("client.field"); //$NON-NLS-1$
return createLocalClient(clientLocClassName, clientLocFieldName, indexName);
return createLocalClient(clientLocClassName, clientLocFieldName, indexName, defaultIndexName);
}

/**
Expand All @@ -173,9 +180,10 @@ public static JestClient createLocalClient(Map<String, String> config, String in
* @param className the class name
* @param fieldName the field name
* @param indexName the name of the ES index
* @param defaultIndexName the name of the default ES index
* @return the ES client
*/
public static JestClient createLocalClient(String className, String fieldName, String indexName) {
public static JestClient createLocalClient(String className, String fieldName, String indexName, String defaultIndexName) {
String clientKey = "local:" + className + '/' + fieldName; //$NON-NLS-1$
synchronized (clients) {
if (clients.containsKey(clientKey)) {
Expand All @@ -186,7 +194,7 @@ public static JestClient createLocalClient(String className, String fieldName, S
Field field = clientLocClass.getField(fieldName);
JestClient client = (JestClient) field.get(null);
clients.put(clientKey, client);
initializeClient(client, indexName);
initializeClient(client, indexName, defaultIndexName);
return client;
} catch (ClassNotFoundException | NoSuchFieldException | SecurityException
| IllegalArgumentException | IllegalAccessException e) {
Expand All @@ -198,14 +206,17 @@ public static JestClient createLocalClient(String className, String fieldName, S

/**
* Called to initialize the storage.
* @param client the jest client
* @param indexName the name of the ES index to initialize
* @param defaultIndexName the default ES index - used to determine which -settings.json file to use
*/
public static void initializeClient(JestClient client, String indexName) {
public static void initializeClient(JestClient client, String indexName, String defaultIndexName) {
try {
client.execute(new Health.Builder().build());
Action<JestResult> action = new IndicesExists.Builder(indexName).build();
JestResult result = client.execute(action);
if (!result.isSucceeded()) {
createIndex(client, indexName, indexName + "-settings.json"); //$NON-NLS-1$
createIndex(client, indexName, defaultIndexName + "-settings.json"); //$NON-NLS-1$
}
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down
Expand Up @@ -55,6 +55,7 @@ public abstract class ApiManagerConfig {
public static final String APIMAN_MANAGER_STORAGE_ES_PASSWORD = "apiman-manager.storage.es.password"; //$NON-NLS-1$
public static final String APIMAN_MANAGER_STORAGE_ES_INITIALIZE = "apiman-manager.storage.es.initialize"; //$NON-NLS-1$
public static final String APIMAN_MANAGER_STORAGE_ES_TIMEOUT = "apiman-manager.storage.es.timeout"; //$NON-NLS-1$
public static final String APIMAN_MANAGER_STORAGE_ES_INDEX_NAME = "apiman-manager.storage.es.index"; //$NON-NLS-1$

public static final String APIMAN_MANAGER_STORAGE_QUERY_TYPE = "apiman-manager.storage-query.type"; //$NON-NLS-1$

Expand All @@ -78,6 +79,7 @@ public abstract class ApiManagerConfig {
public static final String APIMAN_PLUGIN_REGISTRIES = "apiman-manager.plugins.registries"; //$NON-NLS-1$

public static final String DEFAULT_ES_CLUSTER_NAME = "apiman"; //$NON-NLS-1$
public static final String DEFAULT_ES_INDEX_NAME = "apiman_manager"; //$NON-NLS-1$
public static final int DEFAULT_JEST_TIMEOUT = 6000;

private final Configuration config;
Expand Down Expand Up @@ -218,6 +220,10 @@ public String getStorageESPassword() {
public int getStorageESTimeout() {
return config.getInt(APIMAN_MANAGER_STORAGE_ES_TIMEOUT, DEFAULT_JEST_TIMEOUT);
}

public String getStorageESIndexName() {
return config.getString(APIMAN_MANAGER_STORAGE_ES_INDEX_NAME, DEFAULT_ES_INDEX_NAME);
}

/**
* @return true if the elasticsearch index should be initialized if not found
Expand Down
Expand Up @@ -129,7 +129,7 @@
@ApplicationScoped @Alternative
public class EsStorage implements IStorage, IStorageQuery {

private static final String INDEX_NAME = "apiman_manager"; //$NON-NLS-1$
private static final String DEFAULT_INDEX_NAME = "apiman_manager"; //$NON-NLS-1$

private static int guidCounter = 100;

Expand All @@ -141,6 +141,8 @@ public void postConstruct() {
// Kick the encrypter, causing it to be loaded/resolved in CDI
encrypter.encrypt(""); //$NON-NLS-1$
}

private String indexName = DEFAULT_INDEX_NAME;

/**
* Constructor.
Expand All @@ -156,10 +158,10 @@ public void initialize() {
try {
esClient.execute(new Health.Builder().build());
// TODO Do we need a loop to wait for all nodes to join the cluster?
Action<JestResult> action = new IndicesExists.Builder(INDEX_NAME).build();
Action<JestResult> action = new IndicesExists.Builder(getIndexName()).build();
JestResult result = esClient.execute(action);
if (! result.isSucceeded()) {
createIndex(INDEX_NAME);
createIndex(getIndexName());
}
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -927,7 +929,7 @@ public PluginBean getPlugin(String groupId, String artifactId) throws StorageExc
);
SearchSourceBuilder builder = new SearchSourceBuilder().query(qb).size(2);

SearchRequest request = new SearchRequest(INDEX_NAME);
SearchRequest request = new SearchRequest(getIndexName());
request.types("plugin"); //$NON-NLS-1$
request.source(builder);
List<Hit<Map<String,Object>,Void>> hits = listEntities("plugin", builder); //$NON-NLS-1$
Expand Down Expand Up @@ -1652,7 +1654,7 @@ public void deleteMemberships(String userId, String organizationId) throws Stora
if (string.indexOf("query") < 0 || string.indexOf("query") > 7) {
string = "{ \"query\" : " + string + "}";
}
DeleteByQuery deleteByQuery = new DeleteByQuery.Builder(string).addIndex(INDEX_NAME)
DeleteByQuery deleteByQuery = new DeleteByQuery.Builder(string).addIndex(getIndexName())
.addType("roleMembership").build();
try {
JestResult response = esClient.execute(deleteByQuery);
Expand Down Expand Up @@ -1800,7 +1802,7 @@ private void indexEntity(String type, String id, XContentBuilder sourceEntity, b
throws StorageException {
try {
String json = sourceEntity.string();
JestResult response = esClient.execute(new Index.Builder(json).refresh(refresh).index(INDEX_NAME)
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());
Expand All @@ -1820,7 +1822,7 @@ private void indexEntity(String type, String id, XContentBuilder sourceEntity, b
*/
private Map<String, Object> getEntity(String type, String id) throws StorageException {
try {
JestResult response = esClient.execute(new Get.Builder(INDEX_NAME, id).type(type).build());
JestResult response = esClient.execute(new Get.Builder(getIndexName(), id).type(type).build());
if (!response.isSucceeded()) {
return null;
}
Expand All @@ -1840,7 +1842,7 @@ private List<Hit<Map<String, Object>, Void>> listEntities(String type,
SearchSourceBuilder searchSourceBuilder) throws StorageException {
try {
String query = searchSourceBuilder.toString();
Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType(type).build();
Search search = new Search.Builder(query).addIndex(getIndexName()).addType(type).build();
SearchResult response = esClient.execute(search);
@SuppressWarnings({ "rawtypes", "unchecked" })
List<Hit<Map<String, Object>, Void>> thehits = (List) response.getHits(Map.class);
Expand All @@ -1858,7 +1860,7 @@ private List<Hit<Map<String, Object>, Void>> listEntities(String type,
*/
private void deleteEntity(String type, String id) throws StorageException {
try {
JestResult response = esClient.execute(new Delete.Builder(id).index(INDEX_NAME).type(type).build());
JestResult response = esClient.execute(new Delete.Builder(id).index(getIndexName()).type(type).build());
if (!response.isSucceeded()) {
throw new StorageException("Document could not be deleted because it did not exist:" + response.getErrorMessage()); //$NON-NLS-1$
}
Expand All @@ -1880,7 +1882,7 @@ private void updateEntity(String type, String id, XContentBuilder source) throws
try {
String doc = source.string();
/* JestResult response = */esClient.execute(new Index.Builder(doc)
.setParameter(Parameters.OP_TYPE, "index").index(INDEX_NAME).type(type).id(id).build()); //$NON-NLS-1$
.setParameter(Parameters.OP_TYPE, "index").index(getIndexName()).type(type).id(id).build()); //$NON-NLS-1$
} catch (Exception e) {
throw new StorageException(e);
}
Expand Down Expand Up @@ -1957,7 +1959,7 @@ private <T> SearchResultsBean<T> find(SearchCriteriaBean criteria, String type,


String query = builder.toString();
Search search = new Search.Builder(query).addIndex(INDEX_NAME)
Search search = new Search.Builder(query).addIndex(getIndexName())
.addType(type).build();
SearchResult response = esClient.execute(search);
@SuppressWarnings({ "unchecked", "rawtypes" })
Expand Down Expand Up @@ -2423,7 +2425,7 @@ public void remove() {

private void initScroll() throws StorageException {
try {
Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType(entityType)
Search search = new Search.Builder(query).addIndex(getIndexName()).addType(entityType)
.setSearchType(SearchType.SCAN).setParameter(Parameters.SCROLL, "1m").build();
SearchResult response = esClient.execute(search);
scrollId = response.getJsonObject().get("_scroll_id").getAsString();
Expand Down Expand Up @@ -2477,4 +2479,18 @@ private String matchOrgQuery(String organizationId) {
"}";
}

/**
* @return the indexName
*/
public String getIndexName() {
return indexName;
}

/**
* @param indexName the indexName to set
*/
public void setIndexName(String indexName) {
this.indexName = indexName;
}

}
Expand Up @@ -282,6 +282,7 @@ private static JestClient createMetricsJestClient(ManagerApiMicroServiceConfig c
private static EsStorage initES(ManagerApiMicroServiceConfig config, EsStorage esStorage) {
if (sESStorage == null) {
sESStorage = esStorage;
sESStorage.setIndexName(config.getStorageESIndexName());
if (config.isInitializeStorageES()) {
sESStorage.initialize();
}
Expand Down
Expand Up @@ -290,6 +290,7 @@ private static JestClient createMetricsJestClient(WarApiManagerConfig config) {
private static EsStorage initES(WarApiManagerConfig config, EsStorage esStorage) {
if (sESStorage == null) {
sESStorage = esStorage;
sESStorage.setIndexName(config.getStorageESIndexName());
if (config.isInitializeStorageES()) {
sESStorage.initialize();
}
Expand Down
Expand Up @@ -94,7 +94,7 @@ public static void setup() throws Exception {
}

private static JestClient createJestClient() {
return ESClientFactory.createJestClient("http", "localhost", 6500, "apiman_metrics", null, null, true, 6000);
return ESClientFactory.createJestClient("http", "localhost", 6500, "apiman_metrics", null, null, true, 6000, "apiman_metrics");
}

private static void loadTestData() throws Exception {
Expand Down

0 comments on commit b1d609b

Please sign in to comment.