workersPath + "/" + workerNodePrefix, encoder.toBytes(srv));
protected final void populateCache(PathChildrenCache instancesCache, boolean doInvokeListeners) { for (ChildData childData : instancesCache.getCurrentData()) { byte[] data = getWorkerData(childData, workerNodePrefix); if (data == null) continue; String nodeName = extractNodeName(childData); if (!nodeName.startsWith(workerNodePrefix)) continue; int ephSeqVersion = extractSeqNum(nodeName); try { ServiceRecord srv = encoder.fromBytes(childData.getPath(), data); InstanceType instance = createServiceInstance(srv); addToCache(childData.getPath(), instance.getHost(), instance); if (doInvokeListeners) { for (ServiceInstanceStateChangeListener<InstanceType> listener : stateChangeListeners) { listener.onCreate(instance, ephSeqVersion); } } } catch (IOException e) { LOG.error("Unable to decode data for zkpath: {}." + " Ignoring from current instances list..", childData.getPath()); } } }
this.encoder = new RegistryUtils.ServiceRecordMarshal();
RegistryOperations operations = getRegistryOperations(); String path = SliderRegistryUtils.resolvePath(args.path); ServiceRecordMarshal serviceRecordMarshal = new ServiceRecordMarshal(); try { if (args.list) { String name = recordEntry.getKey(); ServiceRecord instance = recordEntry.getValue(); String json = serviceRecordMarshal.toJson(instance); if (destDir == null) { println(name); File jsonFile = new File(destDir, filename); write(jsonFile, serviceRecordMarshal.toBytes(instance), true); write(outFile, serviceRecordMarshal.toBytes(instance), true); } else { println(serviceRecordMarshal.toJson(instance));
RegistryOperations operations = getRegistryOperations(); String path = SliderRegistryUtils.resolvePath(args.path); ServiceRecordMarshal serviceRecordMarshal = new ServiceRecordMarshal(); try { if (args.list) { String name = recordEntry.getKey(); ServiceRecord instance = recordEntry.getValue(); String json = serviceRecordMarshal.toJson(instance); if (destDir == null) { println(name); File jsonFile = new File(destDir, filename); write(jsonFile, serviceRecordMarshal.toBytes(instance), true); write(outFile, serviceRecordMarshal.toBytes(instance), true); } else { println(serviceRecordMarshal.toJson(instance));
this.encoder = new RegistryUtils.ServiceRecordMarshal();
getRegistryDNS().initializeZones(conf); ServiceRecord record = getMarshal().fromBytes("somepath", CONTAINER_RECORD.getBytes()); getRegistryDNS().register(
workersPath + "/" + workerNodePrefix, encoder.toBytes(srv));
@Test public void testAppRegistration() throws Exception { ServiceRecord record = getMarshal().fromBytes("somepath", APPLICATION_RECORD.getBytes()); getRegistryDNS().register(
@Test public void testNegativeLookup() throws Exception { ServiceRecord record = getMarshal().fromBytes("somepath", CONTAINER_RECORD.getBytes()); getRegistryDNS().register(
protected final void updateServiceRecord( ServiceRecord srv, boolean doCheckAcls, boolean closeOnFailure) throws IOException { if (srv.get(UNIQUE_IDENTIFIER) == null) { srv.set(UNIQUE_IDENTIFIER, UNIQUE_ID.toString()); } // waitForInitialCreate must have already been called in registerServiceRecord. try { znode.setData(encoder.toBytes(srv)); if (doCheckAcls) { try { checkAndSetAcls(); } catch (Exception ex) { throw new IOException("Error validating or setting ACLs. " + disableMessage, ex); } } } catch (Exception e) { LOG.error("Unable to update znode with new service record", e); if (closeOnFailure) { CloseableUtils.closeQuietly(znode); } throw (e instanceof IOException) ? (IOException) e : new IOException(e); } }
@Test public void testReverseLookupInLargeNetwork() throws Exception { setRegistryDNS(new RegistryDNS("TestRegistry")); Configuration conf = createConfiguration(); conf.set(RegistryConstants.KEY_DNS_DOMAIN, "dev.test"); conf.set(KEY_DNS_ZONE_SUBNET, "172.17.0.0"); conf.set(KEY_DNS_ZONE_MASK, "255.255.224.0"); conf.setTimeDuration(RegistryConstants.KEY_DNS_TTL, 30L, TimeUnit.SECONDS); getRegistryDNS().setDomainName(conf); getRegistryDNS().initializeZones(conf); ServiceRecord record = getMarshal().fromBytes("somepath", CONTAINER_RECORD.getBytes()); getRegistryDNS().register( "/registry/users/root/services/org-apache-slider/test1/components/" + "ctr-e50-1451931954322-0016-01-000002", record); // start assessing whether correct records are available Record[] recs = assertDNSQuery("19.0.17.172.in-addr.arpa.", Type.PTR, 1); assertEquals("wrong result", "httpd-1.test1.root.dev.test.", ((PTRRecord) recs[0]).getTarget().toString()); }
@Override public void bind(String path, ServiceRecord record, int flags) throws PathNotFoundException, FileAlreadyExistsException, InvalidPathnameException, IOException { // Preserve same overwrite semantics as ZK implementation Preconditions.checkArgument(record != null, "null record"); RegistryTypeUtils.validateServiceRecord(path, record); Path dataPath = formatDataPath(path); Boolean overwrite = ((flags & BindFlags.OVERWRITE) != 0); if (fs.exists(dataPath) && !overwrite) { throw new FileAlreadyExistsException(); } else { // Either the file doesn't exist, or it exists and we're // overwriting. Create overwrites by default and creates parent dirs if // needed. FSDataOutputStream stream = fs.create(dataPath); byte[] bytes = serviceRecordMarshal.toBytes(record); stream.write(bytes); stream.close(); LOG.info("Bound record to path " + dataPath); } }
protected final void populateCache(PathChildrenCache instancesCache, boolean doInvokeListeners) { for (ChildData childData : instancesCache.getCurrentData()) { byte[] data = getWorkerData(childData, workerNodePrefix); if (data == null) continue; String nodeName = extractNodeName(childData); if (!nodeName.startsWith(workerNodePrefix)) continue; int ephSeqVersion = extractSeqNum(nodeName); try { ServiceRecord srv = encoder.fromBytes(childData.getPath(), data); InstanceType instance = createServiceInstance(srv); addToCache(childData.getPath(), instance.getHost(), instance); if (doInvokeListeners) { for (ServiceInstanceStateChangeListener<InstanceType> listener : stateChangeListeners) { listener.onCreate(instance, ephSeqVersion); } } } catch (IOException e) { LOG.error("Unable to decode data for zkpath: {}." + " Ignoring from current instances list..", childData.getPath()); } } }
@Override public ApplicationId getApplicationId() { for (ChildData childData : instancesCache.getCurrentData()) { byte[] data = getWorkerData(childData, WORKER_PREFIX); if (data == null) continue; ServiceRecord sr = null; try { sr = encoder.fromBytes(childData.getPath(), data); } catch (IOException e) { LOG.error("Unable to decode data for zkpath: {}." + " Ignoring from current instances list..", childData.getPath()); continue; } String containerStr = sr.get(HiveConf.ConfVars.LLAP_DAEMON_CONTAINER_ID.varname); if (containerStr == null || containerStr.isEmpty()) continue; return ContainerId.fromString(containerStr).getApplicationAttemptId().getApplicationId(); } return null; } }
@Test public void testMultiARecord() throws Exception { ServiceRecord record = getMarshal().fromBytes("somepath", CONTAINER_RECORD.getBytes()); ServiceRecord record2 = getMarshal().fromBytes("somepath", CONTAINER_RECORD2.getBytes()); getRegistryDNS().register( "/registry/users/root/services/org-apache-slider/test1/components/" + "ctr-e50-1451931954322-0016-01-000002", record); getRegistryDNS().register( "/registry/users/root/services/org-apache-slider/test1/components/" + "ctr-e50-1451931954322-0016-01-000003", record2); // start assessing whether correct records are available Record[] recs = assertDNSQuery("httpd.test1.root.dev.test.", 2); assertTrue("not an ARecord", recs[0] instanceof ARecord); assertTrue("not an ARecord", recs[1] instanceof ARecord); }
@Test public void testMissingReverseLookup() throws Exception { ServiceRecord record = getMarshal().fromBytes("somepath", CONTAINER_RECORD.getBytes()); getRegistryDNS().register( "/registry/users/root/services/org-apache-slider/test1/components/" + "ctr-e50-1451931954322-0016-01-000002", record); // start assessing whether correct records are available Name name = Name.fromString("19.1.17.172.in-addr.arpa."); Record question = Record.newRecord(name, Type.PTR, DClass.IN); Message query = Message.newQuery(question); OPTRecord optRecord = new OPTRecord(4096, 0, 0, Flags.DO, null); query.addRecord(optRecord, Section.ADDITIONAL); byte[] responseBytes = getRegistryDNS().generateReply(query, null); Message response = new Message(responseBytes); assertEquals("Missing record should be: ", Rcode.NXDOMAIN, response.getRcode()); }
@Override public ServiceRecord resolve(String path) throws PathNotFoundException, NoRecordException, InvalidRecordException, IOException { // Read the entire file into byte array, should be small metadata Long size = fs.getFileStatus(formatDataPath(path)).getLen(); byte[] bytes = new byte[size.intValue()]; FSDataInputStream instream = fs.open(formatDataPath(path)); int bytesRead = instream.read(bytes); instream.close(); if (bytesRead < size) { throw new InvalidRecordException(path, "Expected " + size + " bytes, but read " + bytesRead); } // Unmarshal, check, and return ServiceRecord record = serviceRecordMarshal.fromBytes(path, bytes); RegistryTypeUtils.validateServiceRecord(path, record); return record; }
@Test public void testContainerRegistrationPersistanceAbsent() throws Exception { ServiceRecord record = marshal.fromBytes("somepath", CONTAINER_RECORD_YARN_PERSISTANCE_ABSENT.getBytes()); registryDNS.register( "/registry/users/root/services/org-apache-slider/test1/components/" + "ctr-e50-1451931954322-0016-01-000003", record); Name name = Name.fromString("ctr-e50-1451931954322-0016-01-000002.dev.test."); Record question = Record.newRecord(name, Type.A, DClass.IN); Message query = Message.newQuery(question); byte[] responseBytes = registryDNS.generateReply(query, null); Message response = new Message(responseBytes); assertEquals("Excepting NXDOMAIN as Record must not have regsisterd wrong", Rcode.NXDOMAIN, response.getRcode()); }
@Test public void testRecordTTL() throws Exception { ServiceRecord record = getMarshal().fromBytes("somepath", CONTAINER_RECORD.getBytes()); getRegistryDNS().register( "/registry/users/root/services/org-apache-slider/test1/components/" + "ctr-e50-1451931954322-0016-01-000002", record); // start assessing whether correct records are available Record[] recs = assertDNSQuery( "ctr-e50-1451931954322-0016-01-000002.dev.test."); assertEquals("wrong result", "172.17.0.19", ((ARecord) recs[0]).getAddress().getHostAddress()); assertEquals("wrong ttl", 30L, recs[0].getTTL()); recs = assertDNSQuery("httpd-1.test1.root.dev.test.", 1); assertTrue("not an ARecord", recs[0] instanceof ARecord); assertEquals("wrong ttl", 30L, recs[0].getTTL()); }