use of org.apache.solr.common.util.Pair in project lucene-solr by apache.
the class TestLockTree method testLocks.
public void testLocks() throws Exception {
LockTree lockTree = new LockTree();
Lock coll1Lock = lockTree.getSession().lock(CollectionAction.CREATE, Arrays.asList("coll1"));
assertNotNull(coll1Lock);
assertNull("Should not be able to lock coll1/shard1", lockTree.getSession().lock(CollectionAction.BALANCESHARDUNIQUE, Arrays.asList("coll1", "shard1")));
assertNull(lockTree.getSession().lock(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2")));
coll1Lock.unlock();
Lock shard1Lock = lockTree.getSession().lock(CollectionAction.BALANCESHARDUNIQUE, Arrays.asList("coll1", "shard1"));
assertNotNull(shard1Lock);
shard1Lock.unlock();
Lock replica1Lock = lockTree.getSession().lock(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2"));
assertNotNull(replica1Lock);
List<Pair<CollectionAction, List<String>>> operations = new ArrayList<>();
operations.add(new Pair<>(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2")));
operations.add(new Pair<>(MODIFYCOLLECTION, Arrays.asList("coll1")));
operations.add(new Pair<>(SPLITSHARD, Arrays.asList("coll1", "shard1")));
operations.add(new Pair<>(SPLITSHARD, Arrays.asList("coll2", "shard2")));
operations.add(new Pair<>(MODIFYCOLLECTION, Arrays.asList("coll2")));
operations.add(new Pair<>(DELETEREPLICA, Arrays.asList("coll2", "shard1")));
List<Set<String>> orderOfExecution = Arrays.asList(ImmutableSet.of("coll1/shard1/core_node2", "coll2/shard2"), ImmutableSet.of("coll1", "coll2"), ImmutableSet.of("coll1/shard1", "coll2/shard1"));
lockTree = new LockTree();
for (int counter = 0; counter < orderOfExecution.size(); counter++) {
LockTree.Session session = lockTree.getSession();
List<Pair<CollectionAction, List<String>>> completedOps = new CopyOnWriteArrayList<>();
List<Lock> locks = new CopyOnWriteArrayList<>();
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < operations.size(); i++) {
Pair<CollectionAction, List<String>> operation = operations.get(i);
final Lock lock = session.lock(operation.first(), operation.second());
if (lock != null) {
Thread thread = new Thread(getRunnable(completedOps, operation, locks, lock));
threads.add(thread);
thread.start();
}
}
for (Thread thread : threads) thread.join();
if (locks.isEmpty())
throw new RuntimeException("Could not attain lock for anything " + operations);
Set<String> expectedOps = orderOfExecution.get(counter);
log.info("counter : {} , expected : {}, actual : {}", counter, expectedOps, locks);
assertEquals(expectedOps.size(), locks.size());
for (Lock lock : locks) assertTrue("locks : " + locks + " expectedOps : " + expectedOps, expectedOps.contains(lock.toString()));
locks.clear();
for (Pair<CollectionAction, List<String>> completedOp : completedOps) {
operations.remove(completedOp);
}
}
}
use of org.apache.solr.common.util.Pair in project lucene-solr by apache.
the class ZkStateReader method addSecuritynodeWatcher.
private void addSecuritynodeWatcher(final Callable<Pair<byte[], Stat>> callback) throws KeeperException, InterruptedException {
zkClient.exists(SOLR_SECURITY_CONF_PATH, new Watcher() {
@Override
public void process(WatchedEvent event) {
// session events are not change events, and do not remove the watcher
if (EventType.None.equals(event.getType())) {
return;
}
try {
synchronized (ZkStateReader.this.getUpdateLock()) {
LOG.debug("Updating [{}] ... ", SOLR_SECURITY_CONF_PATH);
// remake watch
final Watcher thisWatch = this;
final Stat stat = new Stat();
final byte[] data = getZkClient().getData(SOLR_SECURITY_CONF_PATH, thisWatch, stat, true);
try {
callback.call(new Pair<>(data, stat));
} catch (Exception e) {
LOG.error("Error running collections node listener", e);
}
}
} catch (KeeperException.ConnectionLossException | KeeperException.SessionExpiredException e) {
LOG.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK: [{}]", e.getMessage());
} catch (KeeperException e) {
LOG.error("A ZK error has occurred", e);
throw new ZooKeeperException(ErrorCode.SERVER_ERROR, "", e);
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
LOG.warn("Interrupted", e);
}
}
}, true);
}
use of org.apache.solr.common.util.Pair in project lucene-solr by apache.
the class TestCollectionAPIs method makeCall.
public static Pair<SolrQueryRequest, SolrQueryResponse> makeCall(final ApiBag apiBag, String path, final SolrRequest.METHOD method, final String payload, final CoreContainer cc) throws Exception {
SolrParams queryParams = new MultiMapSolrParams(Collections.emptyMap());
if (path.indexOf('?') > 0) {
String queryStr = path.substring(path.indexOf('?') + 1);
path = path.substring(0, path.indexOf('?'));
queryParams = SolrRequestParsers.parseQueryString(queryStr);
}
final HashMap<String, String> parts = new HashMap<>();
Api api = apiBag.lookup(path, method.toString(), parts);
if (api == null)
throw new RuntimeException("No handler at path :" + path);
SolrQueryResponse rsp = new SolrQueryResponse();
LocalSolrQueryRequest req = new LocalSolrQueryRequest(null, queryParams) {
@Override
public List<CommandOperation> getCommands(boolean validateInput) {
if (payload == null)
return Collections.emptyList();
return ApiBag.getCommandOperations(new StringReader(payload), api.getCommandSchema(), true);
}
@Override
public Map<String, String> getPathTemplateValues() {
return parts;
}
@Override
public String getHttpMethod() {
return method.toString();
}
};
try {
api.call(req, rsp);
} catch (ApiBag.ExceptionWithErrObject e) {
throw new RuntimeException(e.getMessage() + Utils.toJSONString(e.getErrs()), e);
}
return new Pair<>(req, rsp);
}
use of org.apache.solr.common.util.Pair in project lucene-solr by apache.
the class TestInjection method parseValue.
private static Pair<Boolean, Integer> parseValue(String raw) {
Matcher m = ENABLED_PERCENT.matcher(raw);
if (!m.matches())
throw new RuntimeException("No match, probably bad syntax: " + raw);
String val = m.group(1);
String percent = "100";
if (m.groupCount() == 2) {
percent = m.group(2);
}
return new Pair<>(Boolean.parseBoolean(val), Integer.parseInt(percent));
}
Aggregations