@VisibleForTesting static HTableDescriptor createRecordTableDescriptor(String repositoryName, String tableName) { // We have checks on table name in TableManagerImpl -- probably this can go if (tableName.contains(".") || tableName.contains(":")) { throw new IllegalArgumentException("Repository table name cannot contain periods or colons"); } String hbaseTableName = RepoAndTableUtil.getHBaseTableName(repositoryName, tableName); HTableDescriptor recordTableDescriptor = new HTableDescriptor(hbaseTableName); recordTableDescriptor.addFamily(DATA_CF); recordTableDescriptor.setValue(TABLE_TYPE_PROPERTY, TABLE_TYPE_RECORD); RepoAndTableUtil.setRepositoryOwnership(recordTableDescriptor, repositoryName); return recordTableDescriptor; }
public static String getHBaseTableName(String repositoryName, String tableName) { if (! isValidTableName(tableName)) throw new IllegalArgumentException("Bad table name: " + tableName); if (! isValidTableName(repositoryName)) throw new IllegalArgumentException("Bad repository name: " + repositoryName); if (repositoryName.equals(DEFAULT_REPOSITORY)) { // Tables within the default repository are not prefixed with the repository name, because of backwards // compatibility with the pre-multiple-repositories situation. return tableName; } else { return repositoryName + REPOSITORY_TABLE_SEPARATOR + tableName; } }
public static String extractLilyTableName(String repositoryName, String hbaseTableName) { if (isDefaultRepository(repositoryName)) { return hbaseTableName; } else { String prefix = repositoryName + REPOSITORY_TABLE_SEPARATOR; if (!hbaseTableName.startsWith(prefix)) { throw new IllegalArgumentException(String.format("HBase table '%s' does not belong to repository '%s'", hbaseTableName, repositoryName)); } return hbaseTableName.substring(prefix.length()); } }
private String getHBaseTableName(String tableName) { return RepoAndTableUtil.getHBaseTableName(repositoryName, tableName); }
@Override public List<RepositoryTable> getTables() throws InterruptedException, IOException { HBaseAdmin hbaseAdmin = new HBaseAdmin(configuration); List<RepositoryTable> recordTables = Lists.newArrayList(); try { for (HTableDescriptor tableDescriptor : hbaseAdmin.listTables()) { if (LilyHBaseSchema.isRecordTableDescriptor(tableDescriptor) && RepoAndTableUtil.belongsToRepository(tableDescriptor.getNameAsString(), repositoryName)) { String name = RepoAndTableUtil.extractLilyTableName(repositoryName, tableDescriptor.getNameAsString()); recordTables.add(new RepositoryTableImpl(repositoryName, name)); } } } finally { hbaseAdmin.close(); } return recordTables; }
RepoAndTableUtil.setRepositoryOwnership(tableDescr, repositoryName);
@Override public void preDelete(String repositoryName) throws Exception { log.info("Performing repository pre-delete actions for repository " + repositoryName); HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseConf); try { for (HTableDescriptor tableDescriptor : hbaseAdmin.listTables()) { if (repositoryName.equals(RepoAndTableUtil.getOwningRepository(tableDescriptor))) { String tableName = tableDescriptor.getNameAsString(); log.info("Disabling and deleting table " + tableName); hbaseAdmin.disableTable(tableName); hbaseAdmin.deleteTable(tableName); } } } finally { hbaseAdmin.close(); } } }
@Override public final void processEvents(List<SepEvent> events) { List<LilySepEvent> lilyEvents = new ArrayList<LilySepEvent>(events.size()); for (SepEvent event : events) { if (event.getPayload() == null) { // The event is either not from a Lily table or not a normal record operation. continue; } String[] repoAndTable = RepoAndTableUtil.getRepositoryAndTable(Bytes.toString(event.getTable())); IdGenerator idGenerator = null; try { idGenerator = repositoryManager.getRepository(repoAndTable[0]).getIdGenerator(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } catch (RepositoryUnavailableException e) { log.warn(String.format("Skipping an event because repository is not available. " + "Repository: %s, table: %s, row: %s", repoAndTable[0], repoAndTable[1], Bytes.toStringBinary(event.getRow()))); } catch (RepositoryException e) { throw new RuntimeException(e); } lilyEvents.add(new LilySepEvent(idGenerator, repoAndTable[0], repoAndTable[1], event.getTable(), event.getRow(), event.getKeyValues(), event.getPayload())); } processLilyEvents(lilyEvents); }
public synchronized EventPublisher getEventPublisher(String repositoryName, String tableName) throws IOException, InterruptedException { String hbaseTableName = RepoAndTableUtil.getHBaseTableName(repositoryName, tableName); if (!eventPublishers.containsKey(hbaseTableName)) { eventPublishers.put(hbaseTableName, createEventPublisher(repositoryName, tableName)); } return eventPublishers.get(hbaseTableName); }
@Override public void dropTable(String tableName) throws InterruptedException, IOException { if (Table.RECORD.name.equals(tableName)) { throw new IllegalArgumentException("Can't delete the default record table"); } HBaseAdmin hbaseAdmin = new HBaseAdmin(configuration); String hbaseTableName = RepoAndTableUtil.getHBaseTableName(repositoryName, tableName); try { if (hbaseAdmin.tableExists(hbaseTableName) && LilyHBaseSchema.isRecordTableDescriptor(hbaseAdmin.getTableDescriptor(Bytes.toBytes(hbaseTableName)))) { hbaseAdmin.disableTable(hbaseTableName); hbaseAdmin.deleteTable(hbaseTableName); } else { throw new IllegalArgumentException( String.format("Table '%s' is not a valid record table (HBase table name: '%s')", tableName, hbaseTableName)); } } finally { hbaseAdmin.close(); } }
@Override public LTable getTable(String tableName) throws InterruptedException, RepositoryException { if (! RepoAndTableUtil.isValidTableName(tableName)) throw new IllegalArgumentException("Not a valid table name: " + tableName); return repositoryManager.getRepository(repoTableKey.getRepositoryName(), tableName); }
@Override public RepositoryTable createTable(TableCreateDescriptor descriptor) throws InterruptedException, IOException { if (!RepoAndTableUtil.isValidTableName(descriptor.getName())) { throw new IllegalArgumentException(String.format("'%s' is not a valid table name. " + RepoAndTableUtil.VALID_NAME_EXPLANATION, descriptor.getName())); } if (tableExists(descriptor.getName())) { throw new IllegalArgumentException(String.format("Table '%s' already exists", descriptor.getName())); } LilyHBaseSchema.getRecordTable(tableFactory, repositoryName, descriptor.getName(), descriptor.getSplitKeys()); return new RepositoryTableImpl(repositoryName, descriptor.getName()); }