You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@accumulo.apache.org by GitBox <gi...@apache.org> on 2022/05/20 17:25:20 UTC

[GitHub] [accumulo] milleruntime opened a new pull request, #2718: Consolidate Root serialization code

milleruntime opened a new pull request, #2718:
URL: https://github.com/apache/accumulo/pull/2718

   * Attempt to disambiguate the term "metadata" by renaming serialization classes
   * Rename RootGcCandidates to RootGcCandidatesSerializer and move to core
   * Rename RootTabletMetadata RootTabletSerializer


-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] ctubbsii commented on a diff in pull request #2718: Consolidate Root serialization code

Posted by GitBox <gi...@apache.org>.
ctubbsii commented on code in PR #2718:
URL: https://github.com/apache/accumulo/pull/2718#discussion_r883208030


##########
server/base/src/main/java/org/apache/accumulo/server/metadata/RootGcCandidates.java:
##########
@@ -31,85 +29,73 @@
 import org.apache.accumulo.core.metadata.StoredTabletFile;
 import org.apache.hadoop.fs.Path;
 
-import com.google.common.base.Preconditions;
 import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 
 public class RootGcCandidates {
-  private static final Gson GSON = new GsonBuilder().create();
+  // Version 1. Released with Accumulo version 2.1.0
+  private static final int VERSION = 1;
+
+  private final Gson gson = new Gson();
+  private final Data data;
 
   // This class is used to serialize and deserialize root tablet metadata using GSon. Any changes to
   // this class must consider persisted data.
-  private static class GSonData {
-    int version = 1;
-
-    // SortedMap<dir path, SortedSet<file name>>
-    SortedMap<String,SortedSet<String>> candidates;
+  private static class Data {
+    private final int version;
+
+    /*
+     * The root tablet will only have a single dir on each volume. Therefore, root file paths will
+     * have a small set of unique prefixes. The following map is structured to avoid storing the
+     * same dir prefix over and over in JSon and java.
+     *
+     * SortedMap<dir path, SortedSet<file name>>
+     */
+    private final SortedMap<String,SortedSet<String>> candidates;
+
+    public Data(int version, SortedMap<String,SortedSet<String>> candidates) {
+      this.version = version;
+      this.candidates = candidates;
+    }
   }
 
-  /*
-   * The root tablet will only have a single dir on each volume. Therefore root file paths will have
-   * a small set of unique prefixes. The following map is structured to avoid storing the same dir
-   * prefix over and over in JSon and java.
-   *
-   * SortedMap<dir path, SortedSet<file name>>
-   */
-  private SortedMap<String,SortedSet<String>> candidates;
-
   public RootGcCandidates() {
-    this.candidates = new TreeMap<>();
+    this.data = new Data(VERSION, new TreeMap<>());
   }
 
-  private RootGcCandidates(SortedMap<String,SortedSet<String>> candidates) {
-    this.candidates = candidates;
+  public RootGcCandidates(String jsonString) {
+    this.data = gson.fromJson(jsonString, Data.class);
+    checkArgument(data.version == VERSION, "Invalid Root Table GC Candidates JSON version %s",
+        data.version);
+    data.candidates.forEach((parent, files) -> {
+      checkArgument(!parent.isBlank(), "Blank parent dir in %s", data.candidates);
+      checkArgument(!files.isEmpty(), "Empty files for dir %s", parent);
+    });
   }
 
-  public void add(Iterator<StoredTabletFile> refs) {
-    refs.forEachRemaining(ref -> {
-      String parent = ref.getPath().getParent().toString();
-      candidates.computeIfAbsent(parent, k -> new TreeSet<>()).add(ref.getFileName());
-    });
+  public void add(Stream<StoredTabletFile> refs) {
+    refs.forEach(ref -> data.candidates
+        .computeIfAbsent(ref.getPath().getParent().toString(), k -> new TreeSet<>())
+        .add(ref.getFileName()));
   }
 
-  public void remove(Collection<String> refs) {
-    refs.forEach(ref -> {
-      Path path = new Path(ref);
-      String parent = path.getParent().toString();
-      String name = path.getName();
-
-      SortedSet<String> names = candidates.get(parent);
-      if (names != null) {
-        names.remove(name);
-        if (names.isEmpty()) {
-          candidates.remove(parent);
-        }
-      }
-    });
+  public void remove(Stream<String> refs) {
+    refs.map(Path::new).forEach(
+        path -> data.candidates.computeIfPresent(path.getParent().toString(), (key, values) -> {
+          values.remove(path.getName());
+          return values.isEmpty() ? null : values;
+        }));
   }
 
-  public Stream<String> stream() {
-    return candidates.entrySet().stream().flatMap(entry -> {
+  public Stream<String> sortedStream() {

Review Comment:
   Because the caller was sorting, even though it was already sorted. I wanted to make it more clear in the method name and made sure that the method implementation returned a sorted stream.



-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] milleruntime commented on a diff in pull request #2718: Consolidate Root serialization code

Posted by GitBox <gi...@apache.org>.
milleruntime commented on code in PR #2718:
URL: https://github.com/apache/accumulo/pull/2718#discussion_r881976891


##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootGcCandidatesJson.java:
##########
@@ -96,20 +98,6 @@ public Stream<String> stream() {
   }
 
   public String toJson() {
-    GSonData gd = new GSonData();
-    gd.candidates = candidates;
-    return GSON.toJson(gd);
-  }
-
-  public static RootGcCandidates fromJson(String json) {
-    GSonData gd = GSON.fromJson(json, GSonData.class);
-
-    Preconditions.checkArgument(gd.version == 1);
-
-    return new RootGcCandidates(gd.candidates);
-  }
-
-  public static RootGcCandidates fromJson(byte[] json) {
-    return fromJson(new String(json, UTF_8));
+    return GSON.toJson(this);

Review Comment:
   I brought back the static inner classes in [afac496](https://github.com/apache/accumulo/pull/2718/commits/afac49674106a50c19c142204733652f6e77a13a) but made the names a little more descriptive.



-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] milleruntime commented on a diff in pull request #2718: Consolidate Root serialization code

Posted by GitBox <gi...@apache.org>.
milleruntime commented on code in PR #2718:
URL: https://github.com/apache/accumulo/pull/2718#discussion_r881654116


##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootTabletSerializer.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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
+ *
+ *   http://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.core.metadata.schema;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import org.apache.accumulo.core.client.admin.TimeType;
+import org.apache.accumulo.core.data.Mutation;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.metadata.RootTable;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.DataFileColumnFamily;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.ServerColumnFamily;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.TabletColumnFamily;
+
+/**
+ * Serializes the root tablet metadata as Json.
+ */
+public class RootTabletSerializer {
+  /**
+   * This class is only static helper methods.
+   */
+  private RootTabletSerializer() {}
+
+  /**
+   * Assumes the byte Array is UTF8 encoded.
+   */
+  public static RootTabletJson fromJson(byte[] bs) {
+    return new RootTabletJson(new String(bs, UTF_8));
+  }
+
+  /**
+   * Generate initial json for the root tablet metadata. Return the JSON converted to a byte[].
+   */
+  public static byte[] getInitialJson(String dirName, String file) {

Review Comment:
   Not really. My original intent was to split up the static serialization logic from the object being serialized, making 2 classes: the "Serializer" class and the "Json" class.



-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] ctubbsii commented on a diff in pull request #2718: Consolidate Root serialization code

Posted by GitBox <gi...@apache.org>.
ctubbsii commented on code in PR #2718:
URL: https://github.com/apache/accumulo/pull/2718#discussion_r881020599


##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootGcCandidatesJson.java:
##########
@@ -54,14 +50,16 @@ private static class GSonData {
    *
    * SortedMap<dir path, SortedSet<file name>>
    */
-  private SortedMap<String,SortedSet<String>> candidates;
+  private final SortedMap<String,SortedSet<String>> candidates;
 
-  public RootGcCandidates() {
+  public RootGcCandidatesJson() {
     this.candidates = new TreeMap<>();
   }
 
-  private RootGcCandidates(SortedMap<String,SortedSet<String>> candidates) {
-    this.candidates = candidates;
+  public RootGcCandidatesJson(String jsonString) {
+    var rootGcCandidatesJson = GSON.fromJson(jsonString, RootGcCandidatesJson.class);
+    Preconditions.checkArgument(rootGcCandidatesJson.getVersion() == 1);

Review Comment:
   I think you want to compare it to the version above, as in:
   
   ```suggestion
       Preconditions.checkArgument(rootGcCandidatesJson.getVersion() == version);
   ```
   



-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] milleruntime commented on a diff in pull request #2718: Consolidate Root serialization code

Posted by GitBox <gi...@apache.org>.
milleruntime commented on code in PR #2718:
URL: https://github.com/apache/accumulo/pull/2718#discussion_r881976412


##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootTabletSerializer.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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
+ *
+ *   http://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.core.metadata.schema;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import org.apache.accumulo.core.client.admin.TimeType;
+import org.apache.accumulo.core.data.Mutation;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.metadata.RootTable;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.DataFileColumnFamily;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.ServerColumnFamily;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.TabletColumnFamily;
+
+/**
+ * Serializes the root tablet metadata as Json.
+ */
+public class RootTabletSerializer {
+  /**
+   * This class is only static helper methods.
+   */
+  private RootTabletSerializer() {}
+
+  /**
+   * Assumes the byte Array is UTF8 encoded.
+   */
+  public static RootTabletJson fromJson(byte[] bs) {
+    return new RootTabletJson(new String(bs, UTF_8));
+  }
+
+  /**
+   * Generate initial json for the root tablet metadata. Return the JSON converted to a byte[].
+   */
+  public static byte[] getInitialJson(String dirName, String file) {

Review Comment:
   Gone in [afac496](https://github.com/apache/accumulo/pull/2718/commits/afac49674106a50c19c142204733652f6e77a13a)



-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] milleruntime merged pull request #2718: Refactor Root Tablet serialization code

Posted by GitBox <gi...@apache.org>.
milleruntime merged PR #2718:
URL: https://github.com/apache/accumulo/pull/2718


-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] milleruntime commented on a diff in pull request #2718: Consolidate Root serialization code

Posted by GitBox <gi...@apache.org>.
milleruntime commented on code in PR #2718:
URL: https://github.com/apache/accumulo/pull/2718#discussion_r881545193


##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootGcCandidatesJson.java:
##########
@@ -96,20 +98,6 @@ public Stream<String> stream() {
   }
 
   public String toJson() {
-    GSonData gd = new GSonData();
-    gd.candidates = candidates;
-    return GSON.toJson(gd);
-  }
-
-  public static RootGcCandidates fromJson(String json) {
-    GSonData gd = GSON.fromJson(json, GSonData.class);
-
-    Preconditions.checkArgument(gd.version == 1);
-
-    return new RootGcCandidates(gd.candidates);
-  }
-
-  public static RootGcCandidates fromJson(byte[] json) {
-    return fromJson(new String(json, UTF_8));
+    return GSON.toJson(this);

Review Comment:
   Yeah, I wasn't sure about this this as well. I could make the static method take the object. Or maybe if I drop the empty arg constructor, this won't look so suspect.



-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] ctubbsii commented on pull request #2718: Consolidate Root serialization code

Posted by GitBox <gi...@apache.org>.
ctubbsii commented on PR #2718:
URL: https://github.com/apache/accumulo/pull/2718#issuecomment-1139202825

   > > Allowed dropping of gson dependency from server.base
   > 
   > @milleruntime I didn't see a pom change related to this comment in the description
   
   I spoke with @milleruntime in person (well, virtually) while working through my code review, and part of my feedback was that the class shouldn't be relocated to a different jar (which was the reason the pom changed). I put it back in a subsequent commit that I appended to the PR, after discussing it with him.
   
   When this is squash-merged, I can clean up the log message for the commit. If not me, whoever merges it should clean up the commit message.


-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] milleruntime commented on pull request #2718: Consolidate Root serialization code

Posted by GitBox <gi...@apache.org>.
milleruntime commented on PR #2718:
URL: https://github.com/apache/accumulo/pull/2718#issuecomment-1136101562

   @keith-turner @dlmarion @EdColeman @ctubbsii I think this change is good to go, if anyone would like to take a look. I made some updates to the Root Tablet code that @keith-turner did way at the beginning of the development cycle for 2.1.


-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] ctubbsii commented on a diff in pull request #2718: Consolidate Root serialization code

Posted by GitBox <gi...@apache.org>.
ctubbsii commented on code in PR #2718:
URL: https://github.com/apache/accumulo/pull/2718#discussion_r881020598


##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootGcCandidatesJson.java:
##########
@@ -54,14 +50,16 @@ private static class GSonData {
    *
    * SortedMap<dir path, SortedSet<file name>>
    */
-  private SortedMap<String,SortedSet<String>> candidates;
+  private final SortedMap<String,SortedSet<String>> candidates;
 
-  public RootGcCandidates() {
+  public RootGcCandidatesJson() {
     this.candidates = new TreeMap<>();
   }
 
-  private RootGcCandidates(SortedMap<String,SortedSet<String>> candidates) {
-    this.candidates = candidates;
+  public RootGcCandidatesJson(String jsonString) {
+    var rootGcCandidatesJson = GSON.fromJson(jsonString, RootGcCandidatesJson.class);
+    Preconditions.checkArgument(rootGcCandidatesJson.getVersion() == 1);

Review Comment:
   I think you want to compare it to the version above, as in:
   
   ```suggestion
       Preconditions.checkArgument(rootGcCandidatesJson.getVersion() == version);
   ```
   



##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootTabletSerializer.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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
+ *
+ *   http://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.core.metadata.schema;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import org.apache.accumulo.core.client.admin.TimeType;
+import org.apache.accumulo.core.data.Mutation;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.metadata.RootTable;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.DataFileColumnFamily;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.ServerColumnFamily;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.TabletColumnFamily;
+
+/**
+ * Serializes the root tablet metadata as Json.
+ */
+public class RootTabletSerializer {
+  /**
+   * This class is only static helper methods.
+   */
+  private RootTabletSerializer() {}
+
+  /**
+   * Assumes the byte Array is UTF8 encoded.
+   */
+  public static RootTabletJson fromJson(byte[] bs) {
+    return new RootTabletJson(new String(bs, UTF_8));
+  }
+
+  /**
+   * Generate initial json for the root tablet metadata. Return the JSON converted to a byte[].
+   */
+  public static byte[] getInitialJson(String dirName, String file) {

Review Comment:
   Do we even need this class? It's a level of indirection that doesn't really seem to add much. The from method can be inline'd or moved into the RootTabletJson class.
   
   
   
   The getInitialJson class can't be used very often, and can probably be moved into some related class.



##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootTabletJson.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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
+ *
+ *   http://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.core.metadata.schema;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.util.Arrays;
+import java.util.EnumSet;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.apache.accumulo.core.data.ArrayByteSequence;
+import org.apache.accumulo.core.data.ByteSequence;
+import org.apache.accumulo.core.data.ColumnUpdate;
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.Mutation;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.metadata.RootTable;
+import org.apache.hadoop.io.Text;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+/**
+ * This class is used to serialize and deserialize root tablet metadata using GSon. Any changes to
+ * this class must consider persisted data. The only data stored about the Root Table is the
+ * COLUMN_FAMILY, COLUMN_QUALIFIER and VALUE. The data is mapped using Strings as follows:
+ *
+ * {@code Map<column_family, Map<column_qualifier, value>> columnValues = new TreeMap<>();}
+ *
+ * @since 2.1.0
+ */
+public class RootTabletJson {
+  // JSON Mapping Version 1. Released with Accumulo version 2.1.0
+  public final int version = 1;
+  // The Root Tablet data to be serialized. Map<column_family, Map<column_qualifier, value>>
+  private final Map<String,Map<String,String>> columnValues = new TreeMap<>();
+
+  private static final ByteSequence CURR_LOC_FAM =
+      new ArrayByteSequence(MetadataSchema.TabletsSection.CurrentLocationColumnFamily.STR_NAME);
+  private static final ByteSequence FUTURE_LOC_FAM =
+      new ArrayByteSequence(MetadataSchema.TabletsSection.FutureLocationColumnFamily.STR_NAME);
+  private static final Logger log = LoggerFactory.getLogger(RootTabletJson.class);
+  static final Gson GSON = new GsonBuilder().create();
+
+  // In memory representation of the Json data
+  private transient final TreeMap<Key,Value> entries;
+
+  public RootTabletJson(String json) {
+    log.info("Creating object from json: {}", json);
+    var rootTabletJson = GSON.fromJson(json, RootTabletJson.class);
+
+    checkArgument(rootTabletJson.getVersion() == 1, "Invalid Root Tablet JSON version");
+
+    String row = RootTable.EXTENT.toMetaRow().toString();
+
+    this.entries = new TreeMap<>();
+
+    // convert each of the JSON values into Key Values in memory
+    rootTabletJson.columnValues.forEach((fam, qualVals) -> {
+      qualVals.forEach((qual, val) -> {
+        Key k = new Key(row, fam, qual, 1);
+        Value v = new Value(val);
+
+        entries.put(k, v);
+      });
+    });
+  }
+
+  public RootTabletJson() {
+    this.entries = new TreeMap<>();
+  }
+
+  public int getVersion() {
+    return version;
+  }
+
+  /**
+   * Convert this class to a {@link TabletMetadata}
+   */
+  public TabletMetadata toTabletMetadata() {
+    return TabletMetadata.convertRow(entries.entrySet().iterator(),
+        EnumSet.allOf(TabletMetadata.ColumnType.class), false);
+  }
+
+  /**
+   * @return a json representation of this object.
+   */
+  public String toJson() {
+    var entrySet = entries.entrySet();
+    for (var entry : entrySet) {
+      String fam = bytesToUtf8(entry.getKey().getColumnFamilyData().toArray());
+      String qual = bytesToUtf8(entry.getKey().getColumnQualifierData().toArray());
+      String val = bytesToUtf8(entry.getValue().get());
+
+      columnValues.computeIfAbsent(fam, k -> new TreeMap<>()).put(qual, val);
+    }
+
+    return GSON.toJson(this);

Review Comment:
   Same comment as above.



##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootGcCandidatesJson.java:
##########
@@ -96,20 +98,6 @@ public Stream<String> stream() {
   }
 
   public String toJson() {
-    GSonData gd = new GSonData();
-    gd.candidates = candidates;
-    return GSON.toJson(gd);
-  }
-
-  public static RootGcCandidates fromJson(String json) {
-    GSonData gd = GSON.fromJson(json, GSonData.class);
-
-    Preconditions.checkArgument(gd.version == 1);
-
-    return new RootGcCandidates(gd.candidates);
-  }
-
-  public static RootGcCandidates fromJson(byte[] json) {
-    return fromJson(new String(json, UTF_8));
+    return GSON.toJson(this);

Review Comment:
   I'm not so sure about this. The old code was robust against changes to this class. You added a warning above... but that's not as safe as enforcing in code the independence of the JSON serialization from changes to this class.



##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootGcCandidatesJson.java:
##########
@@ -96,20 +98,6 @@ public Stream<String> stream() {
   }
 
   public String toJson() {
-    GSonData gd = new GSonData();
-    gd.candidates = candidates;
-    return GSON.toJson(gd);
-  }
-
-  public static RootGcCandidates fromJson(String json) {
-    GSonData gd = GSON.fromJson(json, GSonData.class);
-
-    Preconditions.checkArgument(gd.version == 1);
-
-    return new RootGcCandidates(gd.candidates);
-  }
-
-  public static RootGcCandidates fromJson(byte[] json) {
-    return fromJson(new String(json, UTF_8));
+    return GSON.toJson(this);

Review Comment:
   I'm not so sure about this. The old code was robust against changes to this class. You added a warning above... but that's not as safe as enforcing in code the independence of the JSON serialization from changes to this class.



##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootTabletJson.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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
+ *
+ *   http://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.core.metadata.schema;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.util.Arrays;
+import java.util.EnumSet;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.apache.accumulo.core.data.ArrayByteSequence;
+import org.apache.accumulo.core.data.ByteSequence;
+import org.apache.accumulo.core.data.ColumnUpdate;
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.Mutation;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.metadata.RootTable;
+import org.apache.hadoop.io.Text;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+/**
+ * This class is used to serialize and deserialize root tablet metadata using GSon. Any changes to
+ * this class must consider persisted data. The only data stored about the Root Table is the
+ * COLUMN_FAMILY, COLUMN_QUALIFIER and VALUE. The data is mapped using Strings as follows:
+ *
+ * {@code Map<column_family, Map<column_qualifier, value>> columnValues = new TreeMap<>();}
+ *
+ * @since 2.1.0
+ */
+public class RootTabletJson {
+  // JSON Mapping Version 1. Released with Accumulo version 2.1.0
+  public final int version = 1;
+  // The Root Tablet data to be serialized. Map<column_family, Map<column_qualifier, value>>
+  private final Map<String,Map<String,String>> columnValues = new TreeMap<>();
+
+  private static final ByteSequence CURR_LOC_FAM =
+      new ArrayByteSequence(MetadataSchema.TabletsSection.CurrentLocationColumnFamily.STR_NAME);
+  private static final ByteSequence FUTURE_LOC_FAM =
+      new ArrayByteSequence(MetadataSchema.TabletsSection.FutureLocationColumnFamily.STR_NAME);
+  private static final Logger log = LoggerFactory.getLogger(RootTabletJson.class);
+  static final Gson GSON = new GsonBuilder().create();
+
+  // In memory representation of the Json data
+  private transient final TreeMap<Key,Value> entries;
+
+  public RootTabletJson(String json) {
+    log.info("Creating object from json: {}", json);
+    var rootTabletJson = GSON.fromJson(json, RootTabletJson.class);
+
+    checkArgument(rootTabletJson.getVersion() == 1, "Invalid Root Tablet JSON version");
+
+    String row = RootTable.EXTENT.toMetaRow().toString();
+
+    this.entries = new TreeMap<>();
+
+    // convert each of the JSON values into Key Values in memory
+    rootTabletJson.columnValues.forEach((fam, qualVals) -> {
+      qualVals.forEach((qual, val) -> {
+        Key k = new Key(row, fam, qual, 1);
+        Value v = new Value(val);
+
+        entries.put(k, v);
+      });
+    });
+  }
+
+  public RootTabletJson() {
+    this.entries = new TreeMap<>();
+  }
+
+  public int getVersion() {
+    return version;
+  }
+
+  /**
+   * Convert this class to a {@link TabletMetadata}
+   */
+  public TabletMetadata toTabletMetadata() {
+    return TabletMetadata.convertRow(entries.entrySet().iterator(),
+        EnumSet.allOf(TabletMetadata.ColumnType.class), false);
+  }
+
+  /**
+   * @return a json representation of this object.
+   */
+  public String toJson() {
+    var entrySet = entries.entrySet();
+    for (var entry : entrySet) {
+      String fam = bytesToUtf8(entry.getKey().getColumnFamilyData().toArray());
+      String qual = bytesToUtf8(entry.getKey().getColumnQualifierData().toArray());
+      String val = bytesToUtf8(entry.getValue().get());
+
+      columnValues.computeIfAbsent(fam, k -> new TreeMap<>()).put(qual, val);
+    }
+
+    return GSON.toJson(this);

Review Comment:
   Same comment as above.



##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootTabletSerializer.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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
+ *
+ *   http://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.core.metadata.schema;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import org.apache.accumulo.core.client.admin.TimeType;
+import org.apache.accumulo.core.data.Mutation;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.metadata.RootTable;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.DataFileColumnFamily;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.ServerColumnFamily;
+import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.TabletColumnFamily;
+
+/**
+ * Serializes the root tablet metadata as Json.
+ */
+public class RootTabletSerializer {
+  /**
+   * This class is only static helper methods.
+   */
+  private RootTabletSerializer() {}
+
+  /**
+   * Assumes the byte Array is UTF8 encoded.
+   */
+  public static RootTabletJson fromJson(byte[] bs) {
+    return new RootTabletJson(new String(bs, UTF_8));
+  }
+
+  /**
+   * Generate initial json for the root tablet metadata. Return the JSON converted to a byte[].
+   */
+  public static byte[] getInitialJson(String dirName, String file) {

Review Comment:
   Do we even need this class? It's a level of indirection that doesn't really seem to add much. The from method can be inline'd or moved into the RootTabletJson class.
   
   
   
   The getInitialJson class can't be used very often, and can probably be moved into some related class. Perhaps ZooKeeperInitializer, which is the only caller of this method.



-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] milleruntime commented on a diff in pull request #2718: Consolidate Root serialization code

Posted by GitBox <gi...@apache.org>.
milleruntime commented on code in PR #2718:
URL: https://github.com/apache/accumulo/pull/2718#discussion_r881650728


##########
core/src/main/java/org/apache/accumulo/core/metadata/schema/RootGcCandidatesJson.java:
##########
@@ -96,20 +98,6 @@ public Stream<String> stream() {
   }
 
   public String toJson() {
-    GSonData gd = new GSonData();
-    gd.candidates = candidates;
-    return GSON.toJson(gd);
-  }
-
-  public static RootGcCandidates fromJson(String json) {
-    GSonData gd = GSON.fromJson(json, GSonData.class);
-
-    Preconditions.checkArgument(gd.version == 1);
-
-    return new RootGcCandidates(gd.candidates);
-  }
-
-  public static RootGcCandidates fromJson(byte[] json) {
-    return fromJson(new String(json, UTF_8));
+    return GSON.toJson(this);

Review Comment:
   Nvm, this is not a static method anymore. This is a method on the object itselft so it just returns itself, serialized to a JSON string. It is used in a couple spots:
   During Initialize:
   <pre>
   zoo.putPersistentData(zkInstanceRoot + RootTable.ZROOT_TABLET_GC_CANDIDATES,
           new RootGcCandidatesJson().toJson().getBytes(UTF_8), ZooUtil.NodeExistsPolicy.FAIL);
   </pre>
   In Ample:
   <pre>
   RootGcCandidatesJson rgcc = new RootGcCandidatesJson(currJson);
   log.debug("Root GC candidates before change : {}", currJson);
   mutator.accept(rgcc);
   String newJson = rgcc.toJson();
   log.debug("Root GC candidates after change  : {}", newJson);
   </pre>



-- 
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-unsubscribe@accumulo.apache.org

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


[GitHub] [accumulo] keith-turner commented on a diff in pull request #2718: Consolidate Root serialization code

Posted by GitBox <gi...@apache.org>.
keith-turner commented on code in PR #2718:
URL: https://github.com/apache/accumulo/pull/2718#discussion_r883181843


##########
server/base/src/main/java/org/apache/accumulo/server/metadata/RootGcCandidates.java:
##########
@@ -31,85 +29,73 @@
 import org.apache.accumulo.core.metadata.StoredTabletFile;
 import org.apache.hadoop.fs.Path;
 
-import com.google.common.base.Preconditions;
 import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 
 public class RootGcCandidates {
-  private static final Gson GSON = new GsonBuilder().create();
+  // Version 1. Released with Accumulo version 2.1.0
+  private static final int VERSION = 1;
+
+  private final Gson gson = new Gson();
+  private final Data data;
 
   // This class is used to serialize and deserialize root tablet metadata using GSon. Any changes to
   // this class must consider persisted data.
-  private static class GSonData {
-    int version = 1;
-
-    // SortedMap<dir path, SortedSet<file name>>
-    SortedMap<String,SortedSet<String>> candidates;
+  private static class Data {
+    private final int version;
+
+    /*
+     * The root tablet will only have a single dir on each volume. Therefore, root file paths will
+     * have a small set of unique prefixes. The following map is structured to avoid storing the
+     * same dir prefix over and over in JSon and java.
+     *
+     * SortedMap<dir path, SortedSet<file name>>
+     */
+    private final SortedMap<String,SortedSet<String>> candidates;
+
+    public Data(int version, SortedMap<String,SortedSet<String>> candidates) {
+      this.version = version;
+      this.candidates = candidates;
+    }
   }
 
-  /*
-   * The root tablet will only have a single dir on each volume. Therefore root file paths will have
-   * a small set of unique prefixes. The following map is structured to avoid storing the same dir
-   * prefix over and over in JSon and java.
-   *
-   * SortedMap<dir path, SortedSet<file name>>
-   */
-  private SortedMap<String,SortedSet<String>> candidates;
-
   public RootGcCandidates() {
-    this.candidates = new TreeMap<>();
+    this.data = new Data(VERSION, new TreeMap<>());
   }
 
-  private RootGcCandidates(SortedMap<String,SortedSet<String>> candidates) {
-    this.candidates = candidates;
+  public RootGcCandidates(String jsonString) {
+    this.data = gson.fromJson(jsonString, Data.class);
+    checkArgument(data.version == VERSION, "Invalid Root Table GC Candidates JSON version %s",
+        data.version);
+    data.candidates.forEach((parent, files) -> {
+      checkArgument(!parent.isBlank(), "Blank parent dir in %s", data.candidates);
+      checkArgument(!files.isEmpty(), "Empty files for dir %s", parent);
+    });
   }
 
-  public void add(Iterator<StoredTabletFile> refs) {
-    refs.forEachRemaining(ref -> {
-      String parent = ref.getPath().getParent().toString();
-      candidates.computeIfAbsent(parent, k -> new TreeSet<>()).add(ref.getFileName());
-    });
+  public void add(Stream<StoredTabletFile> refs) {
+    refs.forEach(ref -> data.candidates
+        .computeIfAbsent(ref.getPath().getParent().toString(), k -> new TreeSet<>())
+        .add(ref.getFileName()));
   }
 
-  public void remove(Collection<String> refs) {
-    refs.forEach(ref -> {
-      Path path = new Path(ref);
-      String parent = path.getParent().toString();
-      String name = path.getName();
-
-      SortedSet<String> names = candidates.get(parent);
-      if (names != null) {
-        names.remove(name);
-        if (names.isEmpty()) {
-          candidates.remove(parent);
-        }
-      }
-    });
+  public void remove(Stream<String> refs) {
+    refs.map(Path::new).forEach(
+        path -> data.candidates.computeIfPresent(path.getParent().toString(), (key, values) -> {
+          values.remove(path.getName());
+          return values.isEmpty() ? null : values;
+        }));
   }
 
-  public Stream<String> stream() {
-    return candidates.entrySet().stream().flatMap(entry -> {
+  public Stream<String> sortedStream() {

Review Comment:
   Why change this to a sorted stream?



-- 
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-unsubscribe@accumulo.apache.org

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