keith-turner commented on code in PR #4415:
URL: https://github.com/apache/accumulo/pull/4415#discussion_r1597268966


##########
server/base/src/test/java/org/apache/accumulo/server/metadata/TestAmple.java:
##########
@@ -0,0 +1,262 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.accumulo.server.metadata;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.SortedMap;
+import java.util.function.BiPredicate;
+import java.util.function.Consumer;
+
+import org.apache.accumulo.core.client.AccumuloClient;
+import org.apache.accumulo.core.client.BatchWriter;
+import org.apache.accumulo.core.client.ConditionalWriter;
+import org.apache.accumulo.core.client.IteratorSetting;
+import org.apache.accumulo.core.client.Scanner;
+import org.apache.accumulo.core.client.TableNotFoundException;
+import org.apache.accumulo.core.client.admin.NewTableConfiguration;
+import org.apache.accumulo.core.client.admin.TabletAvailability;
+import org.apache.accumulo.core.client.admin.TabletInformation;
+import org.apache.accumulo.core.client.admin.TimeType;
+import org.apache.accumulo.core.clientImpl.ClientContext;
+import org.apache.accumulo.core.conf.SiteConfiguration;
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.Mutation;
+import org.apache.accumulo.core.data.Range;
+import org.apache.accumulo.core.data.TableId;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.dataImpl.KeyExtent;
+import org.apache.accumulo.core.iterators.user.WholeRowIterator;
+import org.apache.accumulo.core.lock.ServiceLock;
+import org.apache.accumulo.core.metadata.AccumuloTable;
+import org.apache.accumulo.core.metadata.schema.Ample;
+import 
org.apache.accumulo.core.metadata.schema.Ample.ConditionalTabletsMutator;
+import org.apache.accumulo.core.metadata.schema.Ample.DataLevel;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection;
+import 
org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.ServerColumnFamily;
+import org.apache.accumulo.core.metadata.schema.MetadataTime;
+import org.apache.accumulo.core.metadata.schema.TabletsMetadata;
+import org.apache.accumulo.core.metadata.schema.TabletsMetadata.TableOptions;
+import org.apache.accumulo.core.security.Authorizations;
+import org.apache.accumulo.core.util.ColumnFQ;
+import org.apache.accumulo.server.ServerContext;
+import org.apache.hadoop.io.Text;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Supplier;
+import com.google.common.collect.MoreCollectors;
+
+public class TestAmple {
+
+  private static final ConditionalWriterInterceptor EMPTY_INTERCEPTOR =
+      new ConditionalWriterInterceptor() {};
+
+  private static final BiPredicate<Key,Value> INCLUDE_ALL_COLUMNS = (k, v) -> 
true;
+
+  public static BiPredicate<Key,Value> matches(ColumnFQ column) {
+    return (k, v) -> column.equals(new ColumnFQ(k));
+  }
+
+  public static BiPredicate<Key,Value> not(ColumnFQ column) {
+    return matches(column).negate();
+  }
+
+  public static Ample create(ServerContext context, Map<DataLevel,String> 
tables,
+      Supplier<ConditionalWriterInterceptor> cwInterceptor) {
+    return new TestServerAmpleImpl(context, tables, cwInterceptor);
+  }
+
+  public static Ample create(ServerContext context, Map<DataLevel,String> 
tables) {
+    return create(context, tables, () -> EMPTY_INTERCEPTOR);
+  }
+
+  public static class TestServerAmpleImpl extends ServerAmpleImpl {
+
+    private final Map<DataLevel,String> tables;
+    private final Supplier<ConditionalWriterInterceptor> cwInterceptor;
+
+    private TestServerAmpleImpl(ServerContext context, Map<DataLevel,String> 
tables,
+        Supplier<ConditionalWriterInterceptor> cwInterceptor) {
+      super(context, tables::get);
+      this.tables = Map.copyOf(tables);
+      this.cwInterceptor = Objects.requireNonNull(cwInterceptor);
+      Preconditions.checkArgument(!tables.containsKey(DataLevel.ROOT));
+      Preconditions.checkArgument(
+          tables.containsKey(DataLevel.USER) || 
tables.containsKey(DataLevel.METADATA));
+    }
+
+    @Override
+    public TabletsMutator mutateTablets() {
+      return new TabletsMutatorImpl(testAmpleServerContext(getContext(), 
this), tables::get);
+    }
+
+    @Override
+    public ConditionalTabletsMutator conditionallyMutateTablets() {
+      return conditionallyMutateTablets(cwInterceptor.get());
+    }
+
+    @Override
+    public AsyncConditionalTabletsMutator
+        conditionallyMutateTablets(Consumer<ConditionalResult> 
resultsConsumer) {
+      return new AsyncConditionalTabletsMutatorImpl(getContext(), 
getTableMapper(),
+          resultsConsumer);
+    }
+
+    @Override
+    protected TableOptions newBuilder() {
+      return TabletsMetadata.builder(testAmpleServerContext(getContext(), 
this), getTableMapper());
+    }
+
+    /**
+     * Create default metadata for a Table
+     *
+     * TODO: Add a way to pass in options for config
+     *
+     * @param tableId The id of the table to create metadata for
+     */
+    public void createMetadata(TableId tableId) {
+      try (var tabletsMutator = mutateTablets()) {
+        var extent = new KeyExtent(tableId, null, null);
+        var tabletMutator = tabletsMutator.mutateTablet(extent);
+        String dirName = ServerColumnFamily.DEFAULT_TABLET_DIR_NAME;
+        tabletMutator.putPrevEndRow(extent.prevEndRow());
+        tabletMutator.putDirName(dirName);
+        tabletMutator.putTime(new MetadataTime(0, TimeType.MILLIS));
+        tabletMutator.putTabletAvailability(TabletAvailability.HOSTED);
+        tabletMutator.mutate();
+      } catch (Exception e) {
+        throw new IllegalStateException(e);
+      }
+    }
+
+    /**
+     * Create metadata for a table by copying existing metadata for the table 
from the metadata
+     * table in an existing Accumulo instance
+     *
+     * TODO: Add config parents (such as a way to include/exclude what is 
copied, etc)
+     *
+     * @param client The client to scan the existing accumulo metadata table
+     * @param tableId The id of the table to create metadata for
+     * @throws Exception thrown for any error on metadata creation
+     */
+    public void createMetadataFromExisting(AccumuloClient client, TableId 
tableId)
+        throws Exception {
+      createMetadataFromExisting(client, tableId, INCLUDE_ALL_COLUMNS);
+    }
+
+    public void createMetadataFromExisting(AccumuloClient client, TableId 
tableId,
+        BiPredicate<Key,Value> includeColumn) throws Exception {
+      try (Scanner scanner =
+          client.createScanner(AccumuloTable.METADATA.tableName(), 
Authorizations.EMPTY)) {
+        scanner.setRange(TabletsSection.getRange(tableId));
+        IteratorSetting iterSetting = new IteratorSetting(100, 
WholeRowIterator.class);
+        scanner.addScanIterator(iterSetting);
+
+        try (BatchWriter bw =
+            
client.createBatchWriter(getMetadataTableName(DataLevel.of(tableId)))) {
+          for (Entry<Key,Value> entry : scanner) {
+            final SortedMap<Key,Value> decodedRow =
+                WholeRowIterator.decodeRow(entry.getKey(), entry.getValue());
+            Text row = decodedRow.firstKey().getRow();
+            Mutation m = new Mutation(row);
+
+            decodedRow.entrySet().stream().filter(e -> 
includeColumn.test(e.getKey(), e.getValue()))
+                .forEach(e -> m.put(e.getKey().getColumnFamily(), 
e.getKey().getColumnQualifier(),
+                    e.getKey().getColumnVisibilityParsed(), 
e.getKey().getTimestamp(),
+                    e.getValue()));
+            bw.addMutation(m);
+          }
+        }
+      }
+    }
+
+    private ConditionalTabletsMutator
+        conditionallyMutateTablets(ConditionalWriterInterceptor interceptor) {
+      Objects.requireNonNull(interceptor);
+
+      return new ConditionalTabletsMutatorImpl(getContext(), tables::get) {
+
+        @Override
+        protected ConditionalWriter createConditionalWriter(Ample.DataLevel 
dataLevel)
+            throws TableNotFoundException {
+          if (dataLevel == Ample.DataLevel.ROOT) {
+            return super.createConditionalWriter(dataLevel);
+          } else {
+            return new ConditionalWriterDelegator(
+                
getContext().createConditionalWriter(getTableMapper().apply(dataLevel)),
+                interceptor);
+          }
+        }
+      };
+    }
+
+    @Override
+    ServerContext getContext() {
+      // TODO: we can probably cache this

Review Comment:
   Could Suppliers.memoize be used for this?



##########
test/src/main/java/org/apache/accumulo/test/ample/TestAmpleIT.java:
##########
@@ -0,0 +1,191 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.accumulo.test.ample;
+
+import static 
org.apache.accumulo.core.client.ConditionalWriter.Status.ACCEPTED;
+import static org.apache.accumulo.core.client.ConditionalWriter.Status.UNKNOWN;
+import static 
org.apache.accumulo.server.metadata.ConditionalWriterInterceptor.withStatus;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.accumulo.core.client.Accumulo;
+import org.apache.accumulo.core.clientImpl.ClientContext;
+import org.apache.accumulo.core.data.TableId;
+import org.apache.accumulo.core.dataImpl.KeyExtent;
+import org.apache.accumulo.core.fate.FateId;
+import org.apache.accumulo.core.fate.FateInstanceType;
+import org.apache.accumulo.core.metadata.schema.Ample.ConditionalResult.Status;
+import org.apache.accumulo.core.metadata.schema.Ample.DataLevel;
+import org.apache.accumulo.core.metadata.schema.TabletOperationId;
+import org.apache.accumulo.core.metadata.schema.TabletOperationType;
+import org.apache.accumulo.harness.SharedMiniClusterBase;
+import org.apache.accumulo.server.metadata.TestAmple;
+import org.apache.accumulo.server.metadata.TestAmple.TestServerAmpleImpl;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import com.google.common.base.Preconditions;
+
+public class TestAmpleIT extends SharedMiniClusterBase {
+
+  @BeforeAll
+  public static void setup() throws Exception {
+    SharedMiniClusterBase.startMiniCluster();
+  }
+
+  @AfterAll
+  public static void teardown() {
+    SharedMiniClusterBase.stopMiniCluster();
+  }
+
+  @Test
+  public void testCreateMetadataFromExisting() throws Exception {
+
+    String[] tableNames = getUniqueNames(2);
+    String metadataTable = tableNames[0];
+    String userTable = tableNames[1];
+
+    try (ClientContext client =
+        (ClientContext) Accumulo.newClient().from(getClientProps()).build()) {
+      client.tableOperations().create(userTable);
+
+      TestAmple.createMetadataTable(client, metadataTable);
+
+      TableId tableId = 
TableId.of(client.tableOperations().tableIdMap().get(userTable));
+
+      TestServerAmpleImpl ample = (TestServerAmpleImpl) TestAmple
+          .create(getCluster().getServerContext(), Map.of(DataLevel.USER, 
metadataTable));
+
+      ample.createMetadataFromExisting(client, tableId);
+
+      var count = new AtomicInteger();
+      try (var tablets = 
ample.readTablets().forTable(tableId).build().stream()) {
+        tablets.forEach(tm -> {
+          assertNotNull(tm.getTableId());
+          assertNotNull(tm.getExtent());
+          assertNotNull(tm.getTabletAvailability());
+          assertNotNull(tm.getTime());
+          count.incrementAndGet();
+        });
+      }
+      assertEquals(1, count.get());
+    }
+  }
+
+  @Test
+  public void testCreateMetadata() throws Exception {
+    // final var ctx = getCluster().getServerContext();

Review Comment:
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to