@Test public void testAddVersion() throws Exception { Configuration c = UTIL.getConfiguration(); String appId = "addVersion"; byte[] appIdBytes = Bytes.toBytes(appId); byte[] appRow = Bytes.add(Bytes.add(clusterBytes, Constants.SEP_BYTES), Bytes.add(userBytes, Constants.SEP_BYTES), appIdBytes); AppVersionService service = null; Table versionTable = null; try { service = new AppVersionService(hbaseConnection); versionTable = hbaseConnection .getTable(TableName.valueOf(Constants.HISTORY_APP_VERSION_TABLE)); service.addVersion(cluster, user, appId, "v1", 1); Result r = versionTable.get(new Get(appRow)); assertNotNull(r); assertEquals(r.listCells().size(), 1); assertArrayEquals( r.getValue(Constants.INFO_FAM_BYTES, Bytes.toBytes("v1")), Bytes.toBytes(1L)); service.addVersion(cluster, user, appId, "v2", 10); r = versionTable.get(new Get(appRow)); assertNotNull(r); assertEquals(r.listCells().size(), 2); assertArrayEquals( r.getValue(Constants.INFO_FAM_BYTES, Bytes.toBytes("v1")), Bytes.toBytes(1L)); assertArrayEquals( r.getValue(Constants.INFO_FAM_BYTES, Bytes.toBytes("v2")), Bytes.toBytes(10L)); service.addVersion(cluster, user, appId, "v2", 5); r = versionTable.get(new Get(appRow)); assertNotNull(r); assertEquals(r.listCells().size(), 2); assertArrayEquals( r.getValue(Constants.INFO_FAM_BYTES, Bytes.toBytes("v2")), Bytes.toBytes(5L)); service.addVersion(cluster, user, appId, "v1", 11); r = versionTable.get(new Get(appRow)); assertNotNull(r); assertEquals(r.listCells().size(), 2); assertArrayEquals( r.getValue(Constants.INFO_FAM_BYTES, Bytes.toBytes("v1")), Bytes.toBytes(1L)); } finally { try { if (versionTable != null) { versionTable.close(); } } catch (IOException ignore) { } } }
public boolean addVersion(String cluster, String user, String appId, String version, long timestamp) throws IOException { boolean updated = false; byte[] rowKey = getRowKey(cluster, user, appId); byte[] versionCol = Bytes.toBytes(version); int attempts = 0; int maxAttempts = 3; boolean checkForUpdate = true; while (checkForUpdate && attempts < maxAttempts) { attempts++; Put p = null; byte[] expectedValue = null; Get get = new Get(rowKey); get.addColumn(Constants.INFO_FAM_BYTES, versionCol); Table versionsTable = null; try { versionsTable = hbaseConnection .getTable(TableName.valueOf(Constants.HISTORY_APP_VERSION_TABLE)); Result r = versionsTable.get(get); if (r != null && !r.isEmpty()) { byte[] storedValue = r.getValue(Constants.INFO_FAM_BYTES, versionCol); long storedTS = Bytes.toLong(storedValue); if (timestamp < storedTS) { p = new Put(rowKey); p.addColumn(Constants.INFO_FAM_BYTES, versionCol, Bytes.toBytes(timestamp)); expectedValue = storedValue; } else { checkForUpdate = false; } } else { p = new Put(rowKey); p.addColumn(Constants.INFO_FAM_BYTES, versionCol, Bytes.toBytes(timestamp)); } if (p != null) { updated = versionsTable.checkAndPut(rowKey, Constants.INFO_FAM_BYTES, versionCol, expectedValue, p); checkForUpdate = !updated; if (!updated) { LOG.warn("Update of cluster=" + cluster + ", user=" + user + ", app=" + appId + ", version=" + version + " to timestamp " + timestamp + " failed because currently set value changed!" + " (attempt " + attempts + " of " + maxAttempts + ")"); } } } finally { if (versionsTable != null) { versionsTable.close(); } } } return updated; }
AppVersionService { public boolean addVersion(String cluster, String user, String appId, String version, long timestamp) throws IOException { boolean updated = false; byte[] rowKey = getRowKey(cluster, user, appId); byte[] versionCol = Bytes.toBytes(version); int attempts = 0; int maxAttempts = 3; boolean checkForUpdate = true; while (checkForUpdate && attempts < maxAttempts) { attempts++; Put p = null; byte[] expectedValue = null; Get get = new Get(rowKey); get.addColumn(Constants.INFO_FAM_BYTES, versionCol); Table versionsTable = null; try { versionsTable = hbaseConnection .getTable(TableName.valueOf(Constants.HISTORY_APP_VERSION_TABLE)); Result r = versionsTable.get(get); if (r != null && !r.isEmpty()) { byte[] storedValue = r.getValue(Constants.INFO_FAM_BYTES, versionCol); long storedTS = Bytes.toLong(storedValue); if (timestamp < storedTS) { p = new Put(rowKey); p.addColumn(Constants.INFO_FAM_BYTES, versionCol, Bytes.toBytes(timestamp)); expectedValue = storedValue; } else { checkForUpdate = false; } } else { p = new Put(rowKey); p.addColumn(Constants.INFO_FAM_BYTES, versionCol, Bytes.toBytes(timestamp)); } if (p != null) { updated = versionsTable.checkAndPut(rowKey, Constants.INFO_FAM_BYTES, versionCol, expectedValue, p); checkForUpdate = !updated; if (!updated) { LOG.warn("Update of cluster=" + cluster + ", user=" + user + ", app=" + appId + ", version=" + version + " to timestamp " + timestamp + " failed because currently set value changed!" + " (attempt " + attempts + " of " + maxAttempts + ")"); } } } finally { if (versionsTable != null) { versionsTable.close(); } } } return updated; } }
AppVersionService { public boolean addVersion(String cluster, String user, String appId, String version, long timestamp) throws IOException { boolean updated = false; byte[] rowKey = getRowKey(cluster, user, appId); byte[] versionCol = Bytes.toBytes(version); int attempts = 0; int maxAttempts = 3; boolean checkForUpdate = true; while (checkForUpdate && attempts < maxAttempts) { attempts++; Put p = null; byte[] expectedValue = null; Get get = new Get(rowKey); get.addColumn(Constants.INFO_FAM_BYTES, versionCol); Table versionsTable = null; try { versionsTable = hbaseConnection .getTable(TableName.valueOf(Constants.HISTORY_APP_VERSION_TABLE)); Result r = versionsTable.get(get); if (r != null && !r.isEmpty()) { byte[] storedValue = r.getValue(Constants.INFO_FAM_BYTES, versionCol); long storedTS = Bytes.toLong(storedValue); if (timestamp < storedTS) { p = new Put(rowKey); p.addColumn(Constants.INFO_FAM_BYTES, versionCol, Bytes.toBytes(timestamp)); expectedValue = storedValue; } else { checkForUpdate = false; } } else { p = new Put(rowKey); p.addColumn(Constants.INFO_FAM_BYTES, versionCol, Bytes.toBytes(timestamp)); } if (p != null) { updated = versionsTable.checkAndPut(rowKey, Constants.INFO_FAM_BYTES, versionCol, expectedValue, p); checkForUpdate = !updated; if (!updated) { LOG.warn("Update of cluster=" + cluster + ", user=" + user + ", app=" + appId + ", version=" + version + " to timestamp " + timestamp + " failed because currently set value changed!" + " (attempt " + attempts + " of " + maxAttempts + ")"); } } } finally { if (versionsTable != null) { versionsTable.close(); } } } return updated; } AppVersionService(Connection hbaseConnection); }
AppVersionService { public boolean addVersion(String cluster, String user, String appId, String version, long timestamp) throws IOException { boolean updated = false; byte[] rowKey = getRowKey(cluster, user, appId); byte[] versionCol = Bytes.toBytes(version); int attempts = 0; int maxAttempts = 3; boolean checkForUpdate = true; while (checkForUpdate && attempts < maxAttempts) { attempts++; Put p = null; byte[] expectedValue = null; Get get = new Get(rowKey); get.addColumn(Constants.INFO_FAM_BYTES, versionCol); Table versionsTable = null; try { versionsTable = hbaseConnection .getTable(TableName.valueOf(Constants.HISTORY_APP_VERSION_TABLE)); Result r = versionsTable.get(get); if (r != null && !r.isEmpty()) { byte[] storedValue = r.getValue(Constants.INFO_FAM_BYTES, versionCol); long storedTS = Bytes.toLong(storedValue); if (timestamp < storedTS) { p = new Put(rowKey); p.addColumn(Constants.INFO_FAM_BYTES, versionCol, Bytes.toBytes(timestamp)); expectedValue = storedValue; } else { checkForUpdate = false; } } else { p = new Put(rowKey); p.addColumn(Constants.INFO_FAM_BYTES, versionCol, Bytes.toBytes(timestamp)); } if (p != null) { updated = versionsTable.checkAndPut(rowKey, Constants.INFO_FAM_BYTES, versionCol, expectedValue, p); checkForUpdate = !updated; if (!updated) { LOG.warn("Update of cluster=" + cluster + ", user=" + user + ", app=" + appId + ", version=" + version + " to timestamp " + timestamp + " failed because currently set value changed!" + " (attempt " + attempts + " of " + maxAttempts + ")"); } } } finally { if (versionsTable != null) { versionsTable.close(); } } } return updated; } AppVersionService(Connection hbaseConnection); String getLatestVersion(String cluster, String user, String appId); List<VersionInfo> getDistinctVersions(String cluster, String user,
      String appId); boolean addVersion(String cluster, String user, String appId,
      String version, long timestamp); }
AppVersionService { public boolean addVersion(String cluster, String user, String appId, String version, long timestamp) throws IOException { boolean updated = false; byte[] rowKey = getRowKey(cluster, user, appId); byte[] versionCol = Bytes.toBytes(version); int attempts = 0; int maxAttempts = 3; boolean checkForUpdate = true; while (checkForUpdate && attempts < maxAttempts) { attempts++; Put p = null; byte[] expectedValue = null; Get get = new Get(rowKey); get.addColumn(Constants.INFO_FAM_BYTES, versionCol); Table versionsTable = null; try { versionsTable = hbaseConnection .getTable(TableName.valueOf(Constants.HISTORY_APP_VERSION_TABLE)); Result r = versionsTable.get(get); if (r != null && !r.isEmpty()) { byte[] storedValue = r.getValue(Constants.INFO_FAM_BYTES, versionCol); long storedTS = Bytes.toLong(storedValue); if (timestamp < storedTS) { p = new Put(rowKey); p.addColumn(Constants.INFO_FAM_BYTES, versionCol, Bytes.toBytes(timestamp)); expectedValue = storedValue; } else { checkForUpdate = false; } } else { p = new Put(rowKey); p.addColumn(Constants.INFO_FAM_BYTES, versionCol, Bytes.toBytes(timestamp)); } if (p != null) { updated = versionsTable.checkAndPut(rowKey, Constants.INFO_FAM_BYTES, versionCol, expectedValue, p); checkForUpdate = !updated; if (!updated) { LOG.warn("Update of cluster=" + cluster + ", user=" + user + ", app=" + appId + ", version=" + version + " to timestamp " + timestamp + " failed because currently set value changed!" + " (attempt " + attempts + " of " + maxAttempts + ")"); } } } finally { if (versionsTable != null) { versionsTable.close(); } } } return updated; } AppVersionService(Connection hbaseConnection); String getLatestVersion(String cluster, String user, String appId); List<VersionInfo> getDistinctVersions(String cluster, String user,
      String appId); boolean addVersion(String cluster, String user, String appId,
      String version, long timestamp);  }