CheckIndicesOperation extends AbstractIndexOperation { @Override public List<OperationId> execute(MasterContext context, List<MasterOperation> runningOperations) throws Exception { InteractionProtocol protocol = context.getProtocol(); List<String> liveNodes = protocol.getLiveNodes(); List<String> indices = protocol.getIndices(); for (String indexName : indices) { IndexMetaData indexMD = protocol.getIndexMD(indexName); if (indexMD != null) { if ((indexMD.hasDeployError() && isRecoverable(indexMD.getDeployError(), liveNodes.size())) || canAndShouldRegulateReplication(protocol, indexMD)) { protocol.addMasterOperation(new BalanceIndexOperation(indexName)); } } } return null; }  @Override List<OperationId> execute(MasterContext context, List<MasterOperation> runningOperations); @Override void nodeOperationsComplete(MasterContext context, List<OperationResult> results); @Override ExecutionInstruction getExecutionInstruction(List<MasterOperation> runningOperations); @Override String toString();  }
@Test public void testBalanceUnderreplicatedIndex() throws Exception { MasterQueue masterQueue = Mocks.publishMaster(_protocol); List<Node> nodes = Mocks.mockNodes(2); List<NodeQueue> nodeQueues = Mocks.publisNodes(_protocol, nodes); deployIndex(nodes, nodeQueues); assertEquals(0, masterQueue.size()); CheckIndicesOperation checkOperation = new CheckIndicesOperation(); checkOperation.execute(_context, EMPTY_LIST); assertEquals(0, masterQueue.size()); Node node3 = Mocks.mockNode(); Mocks.publisNode(_protocol, node3); checkOperation.execute(_context, EMPTY_LIST); assertEquals(1, masterQueue.size()); }
@Test public void testBalanceOverreplicatedIndex() throws Exception { MasterQueue masterQueue = Mocks.publishMaster(_protocol); List<Node> nodes = Mocks.mockNodes(3); List<NodeQueue> nodeQueues = Mocks.publisNodes(_protocol, nodes); deployIndex(nodes, nodeQueues); assertEquals(0, masterQueue.size()); CheckIndicesOperation balanceOperation = new CheckIndicesOperation(); balanceOperation.execute(_context, EMPTY_LIST); assertEquals(0, masterQueue.size()); IndexMetaData indexMD = _protocol.getIndexMD(_indexName); indexMD.setReplicationLevel(2); _protocol.updateIndexMD(indexMD); balanceOperation.execute(_context, EMPTY_LIST); assertEquals(1, masterQueue.size()); }