@Override public ServiceRecord resolve(String path) throws IOException { byte[] bytes = zkRead(path); ServiceRecord record = serviceRecordMarshal.fromBytes(path, bytes, ServiceRecord.RECORD_TYPE); RegistryTypeUtils.validateServiceRecord(path, record); return record; }
@Override public ServiceRecord resolve(String path) throws IOException { byte[] bytes = zkRead(path); ServiceRecord record = serviceRecordMarshal.fromBytes(path, bytes, ServiceRecord.RECORD_TYPE); RegistryTypeUtils.validateServiceRecord(path, record); return record; }
@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); } }
@Override public void bind(String path, ServiceRecord record, int flags) throws IOException { Preconditions.checkArgument(record != null, "null record"); validatePath(path); // validate the record before putting it RegistryTypeUtils.validateServiceRecord(path, record); if (LOG.isDebugEnabled()) { LOG.debug("Bound at {} : ServiceRecord = {}", path, record); } CreateMode mode = CreateMode.PERSISTENT; byte[] bytes = serviceRecordMarshal.toBytes(record); zkSet(path, mode, bytes, getClientAcls(), ((flags & BindFlags.OVERWRITE) != 0)); }
@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; }
@Override public void bind(String path, ServiceRecord record, int flags) throws IOException { Preconditions.checkArgument(record != null, "null record"); validatePath(path); // validate the record before putting it RegistryTypeUtils.validateServiceRecord(path, record); LOG.info("Bound at {} : {}", path, record); CreateMode mode = CreateMode.PERSISTENT; byte[] bytes = serviceRecordMarshal.toBytes(record); zkSet(path, mode, bytes, getClientAcls(), ((flags & BindFlags.OVERWRITE) != 0)); }
@Test(expected = InvalidRecordException.class) public void testUnmarshallWrongType() throws Throwable { byte[] bytes = "{'type':''}".getBytes(); ServiceRecord serviceRecord = marshal.fromBytes("marshalling", bytes); RegistryTypeUtils.validateServiceRecord("validating", serviceRecord); }
@Test(expected = InvalidRecordException.class) public void testUnmarshallWrongType() throws Throwable { byte[] bytes = "{'type':''}".getBytes(); ServiceRecord serviceRecord = marshal.fromBytes("marshalling", bytes); RegistryTypeUtils.validateServiceRecord("validating", serviceRecord); }
@Test(expected = InvalidRecordException.class) public void testRecordValidationWrongType() throws Throwable { ServiceRecord record = new ServiceRecord(); record.type = "NotAServiceRecordType"; RegistryTypeUtils.validateServiceRecord("validating", record); }
@Test(expected = InvalidRecordException.class) public void testRecordValidationWrongType() throws Throwable { ServiceRecord record = new ServiceRecord(); record.type = "NotAServiceRecordType"; RegistryTypeUtils.validateServiceRecord("validating", record); }
@Test public void testRoundTrip() throws Throwable { String persistence = PersistencePolicies.PERMANENT; ServiceRecord record = createRecord(persistence); record.set("customkey", "customvalue"); record.set("customkey2", "customvalue2"); RegistryTypeUtils.validateServiceRecord("", record); LOG.info(marshal.toJson(record)); byte[] bytes = marshal.toBytes(record); ServiceRecord r2 = marshal.fromBytes("", bytes); assertMatches(record, r2); RegistryTypeUtils.validateServiceRecord("", r2); }
@Test public void testRoundTrip() throws Throwable { String persistence = PersistencePolicies.PERMANENT; ServiceRecord record = createRecord(persistence); record.set("customkey", "customvalue"); record.set("customkey2", "customvalue2"); RegistryTypeUtils.validateServiceRecord("", record); LOG.info(marshal.toJson(record)); byte[] bytes = marshal.toBytes(record); ServiceRecord r2 = marshal.fromBytes("", bytes); assertMatches(record, r2); RegistryTypeUtils.validateServiceRecord("", r2); }
@Test public void testLsParent() throws Throwable { ServiceRecord written = putExampleServiceEntry(ENTRY_PATH, 0); RegistryPathStatus stat = operations.stat(ENTRY_PATH); List<String> children = operations.list(PARENT_PATH); assertEquals(1, children.size()); assertEquals(NAME, children.get(0)); Map<String, RegistryPathStatus> childStats = RegistryUtils.statChildren(operations, PARENT_PATH); assertEquals(1, childStats.size()); assertEquals(stat, childStats.get(NAME)); Map<String, ServiceRecord> records = RegistryUtils.extractServiceRecords(operations, PARENT_PATH, childStats.values()); assertEquals(1, records.size()); ServiceRecord record = records.get(ENTRY_PATH); RegistryTypeUtils.validateServiceRecord(ENTRY_PATH, record); assertMatches(written, record); }
@Test public void testLsParent() throws Throwable { ServiceRecord written = putExampleServiceEntry(ENTRY_PATH, 0); RegistryPathStatus stat = operations.stat(ENTRY_PATH); List<String> children = operations.list(PARENT_PATH); assertEquals(1, children.size()); assertEquals(NAME, children.get(0)); Map<String, RegistryPathStatus> childStats = RegistryUtils.statChildren(operations, PARENT_PATH); assertEquals(1, childStats.size()); assertEquals(stat, childStats.get(NAME)); Map<String, ServiceRecord> records = RegistryUtils.extractServiceRecords(operations, PARENT_PATH, childStats.values()); assertEquals(1, records.size()); ServiceRecord record = records.get(ENTRY_PATH); RegistryTypeUtils.validateServiceRecord(ENTRY_PATH, record); assertMatches(written, record); }