use of io.zulia.server.connection.server.validation.CreateIndexRequestValidator in project zuliasearch by zuliaio.
the class ZuliaIndexManager method createIndex.
public CreateIndexResponse createIndex(CreateIndexRequest request) throws Exception {
// if existing index make sure not to allow changing number of shards
LOG.info(getLogPrefix() + " creating index: " + request);
request = new CreateIndexRequestValidator().validateAndSetDefault(request);
if (!request.hasIndexSettings()) {
throw new IllegalArgumentException("Index settings field is required for create index");
}
NodeWeightComputation nodeWeightComputation = new DefaultNodeWeightComputation(indexService, thisNode, currentOtherNodesActive);
IndexSettings indexSettings = request.getIndexSettings();
if (indexSettings.getNumberOfShards() == 0) {
indexSettings = indexSettings.toBuilder().setNumberOfShards(1).build();
} else if (indexSettings.getNumberOfShards() < 0) {
throw new IllegalArgumentException("Number of shards cannot be negative");
}
String indexName = indexSettings.getIndexName();
IndexSettings existingIndex = indexService.getIndex(indexName);
long currentTimeMillis = System.currentTimeMillis();
if (existingIndex == null) {
indexSettings = indexSettings.toBuilder().setCreateTime(currentTimeMillis).build();
IndexMapping.Builder indexMapping = IndexMapping.newBuilder();
indexMapping.setIndexName(indexName);
indexMapping.setNumberOfShards(indexSettings.getNumberOfShards());
for (int i = 0; i < indexSettings.getNumberOfShards(); i++) {
List<Node> nodes = nodeWeightComputation.getNodesSortedByWeight();
ShardMapping.Builder shardMapping = ShardMapping.newBuilder();
Node primaryNode = nodes.remove(0);
shardMapping.setPrimaryNode(primaryNode);
shardMapping.setShardNumber(i);
nodeWeightComputation.addShard(primaryNode, indexSettings, true);
for (int r = 0; r < nodes.size(); r++) {
if (r < indexSettings.getNumberOfReplicas()) {
Node replicaNode = nodes.get(r);
shardMapping.addReplicaNode(replicaNode);
nodeWeightComputation.addShard(replicaNode, indexSettings, false);
} else {
break;
}
}
indexMapping.addShardMapping(shardMapping);
}
indexService.storeIndexMapping(indexMapping.build());
} else {
if (existingIndex.equals(indexSettings)) {
LOG.info("No changes to existing index <" + indexName + ">");
return CreateIndexResponse.newBuilder().build();
}
if (existingIndex.getNumberOfShards() != indexSettings.getNumberOfShards()) {
throw new IllegalArgumentException("Cannot change shards for existing index");
}
// TODO handle changing of replication factor
if (existingIndex.getNumberOfReplicas() != indexSettings.getNumberOfReplicas()) {
throw new IllegalArgumentException("Cannot change replication factor for existing index yet");
}
}
indexSettings = indexSettings.toBuilder().setUpdateTime(currentTimeMillis).build();
indexService.storeIndex(indexSettings);
CreateIndexRequestFederator createIndexRequestFederator = new CreateIndexRequestFederator(thisNode, currentOtherNodesActive, pool, internalClient, this);
try {
@SuppressWarnings("unused") List<CreateIndexResponse> send = createIndexRequestFederator.send(InternalCreateIndexRequest.newBuilder().setIndexName(indexName).build());
} catch (Exception e) {
if (existingIndex == null) {
LOG.log(Level.SEVERE, "Failed to update index <" + request.getIndexSettings().getIndexName() + ">: ", e);
throw new Exception("Failed to update index <" + request.getIndexSettings().getIndexName() + ">: " + e.getMessage());
} else {
throw new Exception("Failed to create index <" + request.getIndexSettings().getIndexName() + ">: " + e.getMessage());
}
}
return CreateIndexResponse.newBuilder().build();
}
Aggregations