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/24 22:41:40 UTC

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

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