/** * Create or update an entry * @param path path * @param data data * @param acl ACL for path -used when creating a new entry * @param overwrite enable overwrite * @throws IOException * @return true if the entry was created, false if it was simply updated. */ public boolean zkSet(String path, CreateMode mode, byte[] data, List<ACL> acl, boolean overwrite) throws IOException { Preconditions.checkArgument(data != null, "null data"); checkServiceLive(); if (!zkPathExists(path)) { zkCreate(path, mode, data, acl); return true; } else { if (overwrite) { zkUpdate(path, data); return false; } else { throw new FileAlreadyExistsException(path); } } }
@Override public String toString() { return super.toString() + " " + bindingDiagnosticDetails(); }
/** * Diagnostics method to dump a registry robustly. * Any exception raised is swallowed * @param verbose verbose path dump * @return the registry tree */ protected String dumpRegistryRobustly(boolean verbose) { try { ZKPathDumper pathDumper = dumpPath(verbose); return pathDumper.toString(); } catch (Exception e) { // ignore LOG.debug("Ignoring exception: {}", e); } return ""; } }
/** * Create an instance */ protected void createCuratorService() throws IOException { curatorService = new CuratorService("curatorService"); curatorService.init(createRegistryConfiguration()); curatorService.start(); rootACL = RegistrySecurity.WorldReadWriteACL; curatorService.maybeCreate("", CreateMode.PERSISTENT, rootACL, true); }
@Test public void testUseZKServiceForBinding() throws Throwable { CuratorService cs2 = new CuratorService("curator", zookeeper); cs2.init(new Configuration()); cs2.start(); }
/** * Start a curator service instance * @param name name * @param secure flag to indicate the cluster is secure * @return an inited and started curator service */ protected CuratorService startCuratorServiceInstance(String name, boolean secure) { Configuration clientConf = new Configuration(); clientConf.set(KEY_REGISTRY_ZK_ROOT, "/"); clientConf.setBoolean(KEY_REGISTRY_SECURE, secure); describe(LOG, "Starting Curator service"); CuratorService curatorService = new CuratorService(name, secureZK); curatorService.init(clientConf); curatorService.start(); LOG.info("Curator Binding {}", curatorService.bindingDiagnosticDetails()); return curatorService; }
public void verifyNotExists(String path) throws IOException { if (curatorService.zkPathExists(path)) { fail("Path should not exist: " + path); } } }
/** * test that ZK can write as itself * @throws Throwable */ @Test public void testZookeeperCanWrite() throws Throwable { System.setProperty("curator-log-events", "true"); startSecureZK(); CuratorService curator = null; LoginContext login = login(ZOOKEEPER_LOCALHOST, ZOOKEEPER_CLIENT_CONTEXT, keytab_zk); try { logLoginDetails(ZOOKEEPER, login); RegistrySecurity.setZKSaslClientProperties(ZOOKEEPER, ZOOKEEPER_CLIENT_CONTEXT); curator = startCuratorServiceInstance("ZK", true); LOG.info(curator.toString()); addToTeardown(curator); curator.zkMkPath("/", CreateMode.PERSISTENT, false, RegistrySecurity.WorldReadWriteACL); curator.zkList("/"); curator.zkMkPath("/zookeeper", CreateMode.PERSISTENT, false, RegistrySecurity.WorldReadWriteACL); } finally { logout(login); ServiceOperations.stop(curator); } }
@Test(expected = PathNotFoundException.class) public void testUpdateMissing() throws Throwable { curatorService.zkUpdate("/testupdatemissing", getTestBuffer()); }
/** * Create a path if it does not exist. * The check is poll + create; there's a risk that another process * may create the same path before the create() operation is executed/ * propagated to the ZK node polled. * * @param path path to create * @param acl ACL for path -used when creating a new entry * @param createParents flag to trigger parent creation * @return true iff the path was created * @throws IOException */ @VisibleForTesting public boolean maybeCreate(String path, CreateMode mode, List<ACL> acl, boolean createParents) throws IOException { return zkMkPath(path, mode, createParents, acl); }
@Test public void testInsecureClientToZK() throws Throwable { startSecureZK(); userZookeeperToCreateRoot(); RegistrySecurity.clearZKSaslClientProperties(); CuratorService curatorService = startCuratorServiceInstance("insecure client", false); curatorService.zkList("/"); curatorService.zkMkPath("", CreateMode.PERSISTENT, false, RegistrySecurity.WorldReadWriteACL); }
Configuration conf = getConfig(); createEnsembleProvider(); int sessionTimeout = conf.getInt(KEY_REGISTRY_ZK_SESSION_TIMEOUT, DEFAULT_ZK_SESSION_TIMEOUT); securityConnectionDiagnostics = buildSecurityDiagnostics(); framework = builder.build(); framework.start();
@Test public void testCreateTwice() throws Throwable { byte[] buffer = getTestBuffer(); curatorService.zkCreate("/testcreatetwice", CreateMode.PERSISTENT, buffer, rootACL); try { curatorService.zkCreate("/testcreatetwice", CreateMode.PERSISTENT, buffer, rootACL); fail(); } catch (FileAlreadyExistsException e) { } }
/** * Supply the binding information. * This implementation returns a fixed ensemble bonded to * the quorum supplied by {@link #buildConnectionString()} * @return the binding information */ @Override public BindingInformation supplyBindingInformation() { BindingInformation binding = new BindingInformation(); String connectString = buildConnectionString(); binding.ensembleProvider = new FixedEnsembleProvider(connectString); binding.description = "fixed ZK quorum \"" + connectString + "\""; return binding; }
/** * Init the service. * This is where the security bindings are set up. * * @param conf configuration of the service * @throws Exception */ @Override protected void serviceInit(Configuration conf) throws Exception { registryRoot = conf.getTrimmed(KEY_REGISTRY_ZK_ROOT, DEFAULT_ZK_REGISTRY_ROOT); // add the registy service addService(registrySecurity); if (LOG.isDebugEnabled()) { LOG.debug("Creating Registry with root {}", registryRoot); } super.serviceInit(conf); }
/** * Start a curator service instance * @param name name * @param secure flag to indicate the cluster is secure * @return an inited and started curator service */ protected CuratorService startCuratorServiceInstance(String name, boolean secure) { Configuration clientConf = new Configuration(); clientConf.set(KEY_REGISTRY_ZK_ROOT, "/"); clientConf.setBoolean(KEY_REGISTRY_SECURE, secure); describe(LOG, "Starting Curator service"); CuratorService curatorService = new CuratorService(name, secureZK); curatorService.init(clientConf); curatorService.start(); LOG.info("Curator Binding {}", curatorService.bindingDiagnosticDetails()); return curatorService; }
public void verifyNotExists(String path) throws IOException { if (curatorService.zkPathExists(path)) { fail("Path should not exist: " + path); } } }