Tuesday, July 30, 2013

Interview War Stories : Thread Safety; I know everything!


Interview War Stories: Thread Safety; I know everything!



Note: I will use new names not to disclose the original name.

Interview War Stories; I'll borrow this title from (Cracking The Coding Interview - 4th edition).

Sara (the candidate) was a teaching assistant at one of top Computer Science schools at Egypt and worked for about 4 years at a famous multinational telecom and internet organization.
She applied to our company and since the position she applied for targets candidates with zero or one year experience, I decided to ask her harder questions. Someone may say this not fair! I'll reply No, it's very fair :). I decided to test her learning rate. If she stayed for 4 years in a company, She should get much experience than someone with zero or one year experience.

If I asked someone a question and he said explicitly that he didn't know or he didn't use this technique for a long time ago, I'll take it easy and proceed in asking questions or I'll try to help him in some way to answer this question.
Actually, no one knows everything or remember everything.
I'm always asking for thread safety and how to reach thread safety in different scenarios. If the candidate didn't hear about thread safety, It's Ok and I'll proceed in the questions.

I asked Sara a question: How to apply thread safety in Java?
I wanted to discuss with her the different techniques and why she used this technique not the other one and so on.
Sara said with a smile: we will use the synchronized keyword above the method with the troubles and her we got a synchronized method!
Me: good! But, What about if there is only one line inside the method that is not thread safe?
For example: this line will call a function from a library that is not thread safe (i.e. OpenNLP library).
Sara: Thinking ....... then, First solution : I'll enclose this line in a synchronized block.

<code>
void func1()
{
.
.
.
synchronized
{
          // function call to a function that is not thread safe 
}
.
.
.
}
</code>

Sara: Second Solution: I'll define a synchronized function that includes the call of the function that is not thread safe.
Me: Ok, you suggest two solutions! Do you see that the two solutions will apply the thread safety?
Sara: yes!
Me: Think again :)
Sara: yes, surely the two solutions will apply thread safety!
Me: I see that the first solution may cause some problems.
Sara: No, It will not.
Me: Are you sure?
Sara: yes, I'm very sure.
Me: but, I'm saying that it may cause some problems :). Think again!
Sara: Thinking ....... It'll not cause any problems :)
Me: Ok! Consider this case: I'll call the same function that is not a thread safe from another function func2(). How can you solve this?
Sara: Smiling ... the same solution in func1().
Me: Are you sure that this solution will not cause any problems?
Sara: yes. 
Me: but there will be a problem :)
Sara: No, It'll not be any problem! (Replied as she wanted to say "Enough is enough")
Me: Ok, here is the problem. I'll try to discuss.
synchronized keyword in func1() will block the code (or synchronize it) inside func1() and
synchronized keyword in func2() will block the code (or synchronize it) inside func2()
If in my threads I call both func1() and func2(), there will be a probability to call the function that is not thread safe in different contexts! Got it?
Sara: I still didn't see any problem!
Me: discussing more and more and more ... finally she began to relate.
Me: She didn't receive my discussion in a good manner. Actually, she was annoyed because I have the right solution!


Here is the problem with here in the interview:

I see this like that she want to say "I know everything", which is not true for anyone :)

My advice:

Don't be like Sara. I'll not work with someone who sees that his solution is always the right solution and take a long time in arguing about the solution and refuses to be wrong!
Be open for the solutions from your interviewer. Try to get a good ideas and knowledge from any interview. You are not always right.


The solution:

1 - The second solution is the best from my point of view. Always make a wrapper to the library you will use and include a thread safe functions that you will use in your application.
OR
2 - The first solution wants a small edit to work well.
define a static final object in the class that will serve as a locking object only.
if this object is not a static, it will cause problems ( You can think of this alone :) ).
<code>
class cls
{
static final Object syncObj = new Object();
void func1()
{
.
.
.
synchronized(syncObj)
{
          // function call to a function that is not thread safe 
}
.
.
.
}

void func2()
{
.
.
.
synchronized(syncObj)
{
          // function call to a function that is not thread safe 
}
.
.
.
}
}
</code>

I still prefer the second solution and using a wrapper class because It's a well designed solution and much easier.
You still must use first solution or a mix of the two solutions in some scenarios. Consider a library that have a lot of functions that use a shared resource between these function.

Saturday, June 8, 2013

JSON Polymorphic Types with Jackson - Problem and its solution

Motivation:

While searching the web for a solution for my problem, I didn't find one. Searching Google, Bing and StackOverFlow didn't return a complete solution. Because of the strange problem I got, I should search again and again. After seeing a lot of topics talk about JSON Polymorphic Types and JSON documentation, I solved the problem and see it is useful for all of you to know the solution. I believe it is a general solution, so I decided to share it with you.

Problem:

My task was to make a Client API to "X-Service". this Client API will be coded in Java and will take a JSON String and deserialize it. The problems were that: 1) I don't know the manner that this JSON String written by. 2) The JSON String contains Polymorphic Types. 3) The Polymorphic Types don't have a clear unique identifier to know which class to assign while deserilaization process. But I can make an unique identifier that will consist of two fields.

Note: "X-Service" is not the original name. The names changed due to non-disclosure.

Explanation:

I'll go through an explanation with an example of the problem. I'll provide a general example and solution for Polymorphic Types.

The following code example shows Polymorphic Types. You can see this in two classes Cat and Dog and both extends Animal class. The solution is in @JsonTypeInfo and @JsonSubTypes notations.
// input and output:
//   {
//     "animals":
//     [
//       {"type":"dog","name":"Spike","breed":"mutt",
//           "leash_color":"red"},
//       {"type":"cat","name":"Fluffy",
//           "favorite_toy":"spider ring"}
//     ]
//   }

import java.io.File;
import java.util.Collection;

import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonSubTypes.Type;
import org.codehaus.jackson.annotate.JsonTypeInfo;
import org.codehaus.jackson.map.ObjectMapper;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setPropertyNamingStrategy(
        new CamelCaseNamingStrategy());
    Zoo zoo =
      mapper.readValue(new File("inputFile.json"), Zoo.class);
    System.out.println(mapper.writeValueAsString(zoo));
  }
}

class Zoo
{
  public Collection<Animal> animals;
}

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type")
@JsonSubTypes({
    @Type(value = Cat.class, name = "cat"),
    @Type(value = Dog.class, name = "dog") })
abstract class Animal
{
  public String name;
}

class Dog extends Animal
{
  public String breed;
  public String leashColor;
}

class Cat extends Animal
{
  public String favoriteToy;
}

This is due to the default Jackson deserializer will use @JsonTypeInfo and @JsonSubTypes notations to deserialize the JSON String into the right polymorphic objects. But, What is the solution if the input JSON String is:

// input and output:
//   {
//     "animals":
//     [
//       {"type":"pet","subType":"dog","name":"Spike"},
//       {"type":"other","name":"any name"},
//       {"type":"pet","subType":"cat","name":"Fluffy"},
//       {"type":"wild","subType":"lion", "name":"Layth"}
//     ]
//   }
The problem here is that we have new data structures and none of the fields can be annotated as an unique identifier for Polymorphic Types. The new data structure are below.
abstract class Animal
{
  public String name;
}

class Dog extends Animal
{
  public String type = "pet";
  public String subType = "dog";
}

class Cat extends Animal
{
  public String type = "pet";
  public String subType = "cat";
}

class Lion extends Animal
{
  public String type = "wild";
  public String subType = "lion";
}

class OtherAnimals extends Animal
{
  public String type = "other";
}

You may see this strange!! Me too :) Just proceed with the task as your manager said. Don't ask why he made data structures this manner :).

First Step: write a custom serializer class in Jackson manner and embed your logic that will distinguish between polymorphic types in it like this:

class AnimalDeserializer extends StdDeserializer<Animal> {
    private Map<String, Class<? extends Animal>> registry =
            new HashMap<String, Class<? extends Animal>>();
    AnimalDeserializer() {
        super(Animal.class);
    }
    void registerAnimal(String uniqueAttribute1, String uniqueAttribute2
            ,Class<? extends Animal> animalClass) {
        String str = uniqueAttribute1;
        if(uniqueAttribute2 != null)
            str = str + ":" + uniqueAttribute2;
        registry.put(str, animalClass);
    }
    @Override
    public Animal deserialize(
            JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        ObjectMapper mapper = (ObjectMapper) jp.getCodec();
        ObjectNode root = (ObjectNode) mapper.readTree(jp);
        Class<? extends Animal> animalClass = null;
        Iterator<Entry<String, JsonNode>> elementsIterator =
                root.getFields();
        String Type = null;
        String SubType = null;
        while (elementsIterator.hasNext()) {
            Entry<String, JsonNode> element = elementsIterator.next();
            String name = element.getKey();      
            if(name.equals("Type"))
                Type = element.getValue().asText();
            else if(name.equals("SubType"))
                SubType = element.getValue().asText();
        }
        String str = Type;
        if (SubType != null) {
            str = str + ":" + SubType;
        }
        if (registry.containsKey(str)) {
            animalClass = registry.get(str);
        }
        if (animalClass == null) {
            return null;
        }
        return mapper.readValue(root, animalClass);
    }
}
In this class I noticed that "type" and "subType" fields made together a unique identifier. I exploits this feature and embed my logic in the new serializer as in the above class . and in the following code I'll add a SimpleModule that will contain my customized deserializer to register a unique identifiers for each class extends Animal class.
public static void main(String[] args) throws Exception  
  {
AnimalDeserializer  deserializer = new AnimalDeserializer ();
deserializer.registerAPIResultWidget("pet", "dog", Dog.class);
deserializer.registerAPIResultWidget("pet", "cat", Cat.class);
deserializer.registerAPIResultWidget("wild", "lion", Lion.class);
deserializer.registerAPIResultWidget("other", null, OtherAnimal.class);
SimpleModule module = new SimpleModule("PolymorphicAnimalDeserializerModule",
                new Version(1, 0, 0, null));
module.addDeserializer(Animal.class, deserializer);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module); 
Zoo zoo = mapper.readValue(new File("inputFile.json"), Zoo.class);
System.out.println(mapper.writeValueAsString(zoo));
  }
Using this style in thinking in the problem, you can generalize this solution in Polymorphic Types using Jackson.