use of com.yahoo.messagebus.routing.RoutingPolicy in project vespa by vespa-engine.
the class ProtocolRepository method getRoutingPolicy.
/**
* Creates and returns a routing policy that matches the given arguments. If a routing policy has been created
* previously using the exact same parameters, this method will returned that cached instance instead of creating
* another. Not that when you replace a protocol using {@link #putProtocol(Protocol)} the policy cache is cleared.
*
* @param protocolName The name of the protocol whose routing policy to create.
* @param policyName The name of the routing policy to create.
* @param policyParam The parameter to pass to the routing policy constructor.
* @return The created routing policy.
*/
public RoutingPolicy getRoutingPolicy(String protocolName, String policyName, String policyParam) {
String cacheKey = protocolName + "." + policyName + "." + policyParam;
RoutingPolicy ret = routingPolicyCache.get(cacheKey);
if (ret != null) {
return ret;
}
synchronized (this) {
Protocol protocol = getProtocol(protocolName);
if (protocol == null) {
log.log(LogLevel.ERROR, "Protocol '" + protocolName + "' not supported.");
return null;
}
try {
ret = protocol.createPolicy(policyName, policyParam);
} catch (RuntimeException e) {
log.log(LogLevel.ERROR, "Protcol '" + protocolName + "' threw an exception: " + e.getMessage(), e);
return null;
}
if (ret == null) {
log.log(LogLevel.ERROR, "Protocol '" + protocolName + "' failed to create routing policy '" + policyName + "' with parameter '" + policyParam + "'.");
return null;
}
routingPolicyCache.put(cacheKey, ret);
}
return ret;
}
use of com.yahoo.messagebus.routing.RoutingPolicy in project vespa by vespa-engine.
the class MessageBusTestCase method testRoutingPolicyCache.
@Test
public void testRoutingPolicyCache() throws ListenFailedException, UnknownHostException {
Slobrok slobrok = new Slobrok();
String config = "slobrok[1]\nslobrok[0].connectionspec \"" + new Spec("localhost", slobrok.port()).toString() + "\"\n";
SimpleProtocol protocol = new SimpleProtocol();
protocol.addPolicyFactory("Custom", new CustomPolicyFactory());
MessageBus bus = new MessageBus(new RPCNetwork(new RPCNetworkParams().setSlobrokConfigId("raw:" + config)), new MessageBusParams().addProtocol(protocol));
RoutingPolicy all = bus.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null);
assertNotNull(all);
RoutingPolicy ref = bus.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null);
assertNotNull(ref);
assertSame(all, ref);
RoutingPolicy allArg = bus.getRoutingPolicy(SimpleProtocol.NAME, "Custom", "Arg");
assertNotNull(allArg);
assertNotSame(all, allArg);
RoutingPolicy refArg = bus.getRoutingPolicy(SimpleProtocol.NAME, "Custom", "Arg");
assertNotNull(refArg);
assertSame(allArg, refArg);
}
use of com.yahoo.messagebus.routing.RoutingPolicy in project vespa by vespa-engine.
the class ProtocolRepositoryTestCase method requireThatPolicyParamIsPartOfCacheKey.
@Test
public void requireThatPolicyParamIsPartOfCacheKey() {
ProtocolRepository repo = new ProtocolRepository();
SimpleProtocol protocol = new SimpleProtocol();
protocol.addPolicyFactory("Custom", new MyFactory());
repo.putProtocol(protocol);
RoutingPolicy prev = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", "foo");
assertNotNull(prev);
RoutingPolicy next = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", "bar");
assertNotNull(next);
assertNotSame(prev, next);
}
use of com.yahoo.messagebus.routing.RoutingPolicy in project vespa by vespa-engine.
the class ProtocolRepositoryTestCase method requireThatCreatePolicyExceptionIsCaught.
@Test
public void requireThatCreatePolicyExceptionIsCaught() {
ProtocolRepository repo = new ProtocolRepository();
SimpleProtocol protocol = new SimpleProtocol();
protocol.addPolicyFactory("Custom", new SimpleProtocol.PolicyFactory() {
@Override
public RoutingPolicy create(String param) {
throw new RuntimeException();
}
});
repo.putProtocol(protocol);
assertNull(repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null));
}
use of com.yahoo.messagebus.routing.RoutingPolicy in project vespa by vespa-engine.
the class ProtocolRepositoryTestCase method requireThatPolicyIsCached.
@Test
public void requireThatPolicyIsCached() {
ProtocolRepository repo = new ProtocolRepository();
SimpleProtocol protocol = new SimpleProtocol();
protocol.addPolicyFactory("Custom", new MyFactory());
repo.putProtocol(protocol);
RoutingPolicy prev = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null);
assertNotNull(prev);
RoutingPolicy next = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null);
assertNotNull(next);
assertSame(prev, next);
}
Aggregations