myriad-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smare...@apache.org
Subject incubator-myriad git commit: MYRIAD-148 Ability to correctly serialize Optional. By default Option…
Date Mon, 26 Oct 2015 17:33:11 GMT
Repository: incubator-myriad
Updated Branches:
  refs/heads/master 9c7a739f7 -> e0ca4da1d


MYRIAD-148 Ability to correctly serialize Optional. By default Option…

…al<T> serialized value is just "present" and not a real value (if present). Created
custom serializers to serialize value versus state of Optional

This closes: #19
Review: https://github.com/apache/incubator-myriad/pull/19


Project: http://git-wip-us.apache.org/repos/asf/incubator-myriad/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-myriad/commit/e0ca4da1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-myriad/tree/e0ca4da1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-myriad/diff/e0ca4da1

Branch: refs/heads/master
Commit: e0ca4da1d1b8f96a13577d00ecafe6895a7edbb1
Parents: 9c7a739
Author: Yuliya Feldman <yfeldman@maprtech.com>
Authored: Mon Oct 26 10:29:32 2015 -0700
Committer: Santosh Marella <marella@gmail.com>
Committed: Mon Oct 26 10:29:32 2015 -0700

----------------------------------------------------------------------
 .../configuration/MyriadConfiguration.java      |   6 +
 .../MyriadExecutorConfiguration.java            |   6 +
 .../configuration/NodeManagerConfiguration.java |   9 ++
 .../configuration/OptionalSerializer.java       | 126 +++++++++++++++++++
 .../configuration/ServiceConfiguration.java     |  13 +-
 5 files changed, 159 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-myriad/blob/e0ca4da1/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/MyriadConfiguration.java
----------------------------------------------------------------------
diff --git a/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/MyriadConfiguration.java
b/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/MyriadConfiguration.java
index a723cf0..2dc8f02 100644
--- a/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/MyriadConfiguration.java
+++ b/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/MyriadConfiguration.java
@@ -19,12 +19,16 @@
 package com.ebay.myriad.configuration;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
+import org.codehaus.jackson.map.annotate.JsonSerialize;
 import com.google.common.base.Optional;
 import com.google.common.base.Strings;
+
 import org.hibernate.validator.constraints.NotEmpty;
 
 import java.util.Map;
 
+import com.ebay.myriad.configuration.OptionalSerializer.OptionalSerializerString;
+
 /**
  * Myriad Configuration commonly defined in the YML file
  * mesosMaster: 10.0.2.15:5050
@@ -102,9 +106,11 @@ public class MyriadConfiguration {
   private String frameworkRole;
 
   @JsonProperty
+  @JsonSerialize(using = OptionalSerializerString.class)
   private String frameworkUser;
 
   @JsonProperty
+  @JsonSerialize(using = OptionalSerializerString.class)
   private String frameworkSuperUser;
 
   @JsonProperty

http://git-wip-us.apache.org/repos/asf/incubator-myriad/blob/e0ca4da1/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/MyriadExecutorConfiguration.java
----------------------------------------------------------------------
diff --git a/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/MyriadExecutorConfiguration.java
b/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/MyriadExecutorConfiguration.java
index 9744348..3d95859 100644
--- a/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/MyriadExecutorConfiguration.java
+++ b/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/MyriadExecutorConfiguration.java
@@ -18,8 +18,12 @@
  */
 package com.ebay.myriad.configuration;
 
+import com.ebay.myriad.configuration.OptionalSerializer.OptionalSerializerDouble;
+import com.ebay.myriad.configuration.OptionalSerializer.OptionalSerializerString;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.google.common.base.Optional;
+
+import org.codehaus.jackson.map.annotate.JsonSerialize;
 import org.hibernate.validator.constraints.NotEmpty;
 
 /**
@@ -30,6 +34,7 @@ public class MyriadExecutorConfiguration {
      * Translates to -Xmx for the NodeManager JVM.
      */
     @JsonProperty
+    @JsonSerialize(using = OptionalSerializerDouble.class)
     private Double jvmMaxMemoryMB;
 
     @JsonProperty
@@ -37,6 +42,7 @@ public class MyriadExecutorConfiguration {
     private String path;
 
     @JsonProperty
+    @JsonSerialize(using = OptionalSerializerString.class)
     private String nodeManagerUri;
 
     public Optional<Double> getJvmMaxMemoryMB() {

http://git-wip-us.apache.org/repos/asf/incubator-myriad/blob/e0ca4da1/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/NodeManagerConfiguration.java
----------------------------------------------------------------------
diff --git a/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/NodeManagerConfiguration.java
b/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/NodeManagerConfiguration.java
index 84eb219..5398aaf 100644
--- a/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/NodeManagerConfiguration.java
+++ b/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/NodeManagerConfiguration.java
@@ -18,6 +18,11 @@
  */
 package com.ebay.myriad.configuration;
 
+import org.codehaus.jackson.map.annotate.JsonSerialize;
+
+import com.ebay.myriad.configuration.OptionalSerializer.OptionalSerializerBoolean;
+import com.ebay.myriad.configuration.OptionalSerializer.OptionalSerializerDouble;
+import com.ebay.myriad.configuration.OptionalSerializer.OptionalSerializerString;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.google.common.base.Optional;
 
@@ -46,6 +51,7 @@ public class NodeManagerConfiguration {
      * Translates to -Xmx for the NodeManager JVM.
      */
     @JsonProperty
+    @JsonSerialize(using = OptionalSerializerDouble.class)
     private Double jvmMaxMemoryMB;
 
     /**
@@ -53,18 +59,21 @@ public class NodeManagerConfiguration {
      * for NodeManager auxiliary services.
      */
     @JsonProperty
+    @JsonSerialize(using = OptionalSerializerDouble.class)
     private Double cpus;
 
     /**
      * Translates to JAVA_OPTS for the NodeManager JVM.
      */
     @JsonProperty
+    @JsonSerialize(using = OptionalSerializerString.class)
     private String jvmOpts;
 
     /**
      * Determines if cgroups are enabled for NM or not.
      */
     @JsonProperty
+    @JsonSerialize(using = OptionalSerializerBoolean.class)
     private Boolean cgroups;
 
     public Optional<Double> getJvmMaxMemoryMB() {

http://git-wip-us.apache.org/repos/asf/incubator-myriad/blob/e0ca4da1/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/OptionalSerializer.java
----------------------------------------------------------------------
diff --git a/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/OptionalSerializer.java
b/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/OptionalSerializer.java
new file mode 100644
index 0000000..ce7346e
--- /dev/null
+++ b/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/OptionalSerializer.java
@@ -0,0 +1,126 @@
+/**
+ * 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 com.ebay.myriad.configuration;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.codehaus.jackson.JsonFactory;
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.JsonProcessingException;
+import org.codehaus.jackson.map.JsonSerializer;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.SerializerProvider;
+
+import com.google.common.base.Optional;
+
+/**
+ * Custom Serializer that allows to serialize Optional 
+ * today Optional does not serialize value, but just state: "present: true/false"
+ * This class will serialize <T> value instead of state
+ * This is needed for REST APIs and Myriad UI
+ * @param <T>
+ */
+public class OptionalSerializer<T> extends JsonSerializer<Optional<T>>
{
+
+  private static final JsonFactory jsonFactory = new ObjectMapper().getJsonFactory();
+
+  protected ObjectMapper objMapper;
+  
+  public OptionalSerializer() {
+    objMapper = new ObjectMapper(jsonFactory);
+  }
+  
+  @Override
+  public void serialize(Optional<T> value,
+      JsonGenerator jgen, SerializerProvider provider)
+      throws IOException, JsonProcessingException {
+    if (value.isPresent()) {
+      objMapper.writeValue(jgen, value.get());
+    } else {
+      objMapper.writeValue(jgen, "value is absent");
+    }
+  }
+  
+  /**
+   * Custom String serializer
+   *
+   */
+  public static class OptionalSerializerString extends OptionalSerializer<String> {
+    @Override
+    public void serialize(Optional<String> value, 
+        JsonGenerator jgen, SerializerProvider provider) throws IOException,
+        JsonProcessingException {
+      super.serialize(value, jgen, provider);
+    }
+  }
+  
+  /**
+   * Custom Double serializer
+   *
+   */
+  public static class OptionalSerializerDouble extends OptionalSerializer<Double> {
+    @Override
+    public void serialize(Optional<Double> value, 
+        JsonGenerator jgen, SerializerProvider provider) throws IOException,
+        JsonProcessingException {
+      super.serialize(value, jgen, provider);
+    }
+  }
+  
+  /**
+   * Custom Integer serializer
+   *
+   */
+  public static class OptionalSerializerInt extends OptionalSerializer<Integer> {
+    @Override
+    public void serialize(Optional<Integer> value, 
+        JsonGenerator jgen, SerializerProvider provider) throws IOException,
+        JsonProcessingException {
+      super.serialize(value, jgen, provider);
+    }
+  }
+
+  /**
+   * Custom Boolean serializer
+   *
+   */
+  public static class OptionalSerializerBoolean extends OptionalSerializer<Boolean>
{
+    @Override
+    public void serialize(Optional<Boolean> value, 
+        JsonGenerator jgen, SerializerProvider provider) throws IOException,
+        JsonProcessingException {
+      super.serialize(value, jgen, provider);
+    }
+  }
+
+  /**
+   * Custom Map serializer
+   *
+   */
+  public static class OptionalSerializerMap extends OptionalSerializer<Map<?, ?>>
{
+    @Override
+    public void serialize(Optional<Map<?, ?>> value, 
+        JsonGenerator jgen, SerializerProvider provider) throws IOException,
+        JsonProcessingException {
+      super.serialize(value, jgen, provider);
+    }
+
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-myriad/blob/e0ca4da1/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/ServiceConfiguration.java
----------------------------------------------------------------------
diff --git a/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/ServiceConfiguration.java
b/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/ServiceConfiguration.java
index 29eef6d..775d4c0 100644
--- a/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/ServiceConfiguration.java
+++ b/myriad-scheduler/src/main/java/com/ebay/myriad/configuration/ServiceConfiguration.java
@@ -20,10 +20,15 @@ package com.ebay.myriad.configuration;
 
 import java.util.Map;
 
+import org.codehaus.jackson.map.annotate.JsonSerialize;
 import org.hibernate.validator.constraints.NotEmpty;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.ebay.myriad.configuration.OptionalSerializer.OptionalSerializerDouble;
+import com.ebay.myriad.configuration.OptionalSerializer.OptionalSerializerInt;
+import com.ebay.myriad.configuration.OptionalSerializer.OptionalSerializerMap;
+import com.ebay.myriad.configuration.OptionalSerializer.OptionalSerializerString;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.google.common.base.Optional;
 
@@ -43,21 +48,25 @@ public class ServiceConfiguration {
    * Translates to -Xmx for the JVM.
    */
   @JsonProperty
+  @JsonSerialize(using = OptionalSerializerDouble.class)
   protected Double jvmMaxMemoryMB;
 
   /**
    * Amount of CPU share given to  JVM. 
    */
   @JsonProperty
+  @JsonSerialize(using = OptionalSerializerDouble.class)
   protected Double cpus;
 
   /**
    * Translates to jvm opts for the JVM.
    */
   @JsonProperty
+  @JsonSerialize(using = OptionalSerializerString.class)
   protected String jvmOpts;
 
   @JsonProperty
+  @JsonSerialize(using = OptionalSerializerMap.class)
   protected Map<String, Long> ports;
   
   /**
@@ -66,6 +75,7 @@ public class ServiceConfiguration {
    * we can use this one to have a specific implementation
    */
   @JsonProperty
+  @JsonSerialize(using = OptionalSerializerString.class)
   protected String taskFactoryImplName;
   
   @JsonProperty
@@ -76,9 +86,11 @@ public class ServiceConfiguration {
   protected String taskName;
   
   @JsonProperty
+  @JsonSerialize(using = OptionalSerializerInt.class)
   protected Integer maxInstances;
   
   @JsonProperty
+  @JsonSerialize(using = OptionalSerializerString.class)
   protected String command;
   
   @JsonProperty
@@ -128,5 +140,4 @@ public class ServiceConfiguration {
   public String getServiceOpts() {
     return serviceOptsName;
   }
-
 }


Mime
View raw message