Projects >> cloudname >>d7ba08f74188172ba125230fa2dc145785a17edb

Chunk
Conflicting content
    }

<<<<<<< HEAD
=======
    /**
     * List the sub-nodes in ZooKeeper owned by Cloudname.
     * @param nodeList
     */
    public void listRecursively(List nodeList) {
        Util.listRecursively(zk, ZkCoordinatePath.getCloudnameRoot(), null, nodeList);
    }

>>>>>>> 745a07ffec20e7bc0bcfb6ca418aa18a5812d257
    /**
     *  This class builds parameters for ZkCloudname.
     */
Solution content
    }

    /**
     * List the sub-nodes in ZooKeeper owned by Cloudname.
     * @param nodeList
     */
    public void listRecursively(List nodeList) {
        Util.listRecursively(zk, ZkCoordinatePath.getCloudnameRoot(), null, nodeList);
    }

    /**
     *  This class builds parameters for ZkCloudname.
     */
File
ZkCloudname.java
Developer's decision
Version 2
Kind of conflict
Comment
Method declaration
Chunk
Conflicting content
        for (Integer instance : instances) {
            String statusPath = ZkCoordinatePath.getStatusPath(parameters.getCell(), parameters.getUser(),
                    parameters.getService(), instance);
<<<<<<< HEAD
            ZkStatusAndEndpoints statusAndEndpoints = new ZkStatusAndEndpoints.Builder(zk, path).build().load();
=======
            if (! Util.exist(zk, statusPath)) {
                continue;
            }
            ZkStatusAndEndpoints statusAndEndpoints = new ZkStatusAndEndpoints.Builder(zk, statusPath).load().build();
            if (statusAndEndpoints.getServiceStatus().getState() != ServiceState.RUNNING) {
                continue;
            }
>>>>>>> 745a07ffec20e7bc0bcfb6ca418aa18a5812d257
            if (parameters.getEndpointName() == "") {
                statusAndEndpoints.returnAllEndpoints(endpoints);
            } else {
Solution content
        for (Integer instance : instances) {
            String statusPath = ZkCoordinatePath.getStatusPath(parameters.getCell(), parameters.getUser(),
                    parameters.getService(), instance);

            if (! Util.exist(zk, statusPath)) {
                continue;
            }
            ZkStatusAndEndpoints statusAndEndpoints = new ZkStatusAndEndpoints.Builder(zk, statusPath).build().load();
            if (statusAndEndpoints.getServiceStatus().getState() != ServiceState.RUNNING) {
                continue;
            }
            if (parameters.getEndpointName() == "") {
                statusAndEndpoints.returnAllEndpoints(endpoints);
            } else {
File
ZkResolver.java
Developer's decision
Manual
Kind of conflict
If statement
Method invocation
Variable
Chunk
Conflicting content
     * @param endpointsByName This map is populated with the endpoints.
     * @return the ServiceStatus in the data.                                             ´
     */
<<<<<<< HEAD
    private static ServiceStatus deserialize(String data, ObjectMapper objectMapper, Map endpointsByName) throws IOException {

        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createJsonParser(data);
        String statusString = objectMapper.readValue(jp, new TypeReference () {});
        endpointsByName.clear();
        endpointsByName.putAll((Map)objectMapper.readValue(jp, new TypeReference >() {}));
        return ServiceStatus.fromJson(statusString);
=======
    private static ServiceStatus deserialize(
            String data, ObjectMapper objectMapper, Map endpointsByName) {

        JsonFactory jsonFactory = new JsonFactory();
        try {

            JsonParser jp = jsonFactory.createJsonParser(data);
            String statusString = objectMapper.readValue(jp, new TypeReference () {});
            endpointsByName.clear();
            endpointsByName.putAll((Map)objectMapper.readValue(
                        jp, new TypeReference >() {}));
            return ServiceStatus.fromJson(statusString);

        } catch (IOException e) {
            throw new CloudnameException(e);

        }
>>>>>>> 745a07ffec20e7bc0bcfb6ca418aa18a5812d257
    }

    /**
Solution content
     * @param endpointsByName This map is populated with the endpoints.
     * @return the ServiceStatus in the data.                                             ´
     */

    private static ServiceStatus deserialize(
            String data, ObjectMapper objectMapper, Map endpointsByName) throws IOException {

        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createJsonParser(data);
        String statusString = objectMapper.readValue(jp, new TypeReference () {});
        endpointsByName.clear();
        endpointsByName.putAll((Map)objectMapper.readValue(jp,
                new TypeReference >() {}));
        return ServiceStatus.fromJson(statusString);
    }

    /**
File
ZkStatusAndEndpoints.java
Developer's decision
Version 1
Kind of conflict
Method invocation
Method signature
Return statement
Try statement
Variable
Chunk
Conflicting content
        }

            throw new CloudnameException(e);
        }
    }
<<<<<<< HEAD
=======

    /**
     * This class can build ZkStatusAndEndpoints.
     * If you want to create an instance that first claims the coordinate it can be done like this:
     *   ZkStatusAndEndpoints statusAndEndpoints = new ZkStatusAndEndpoints.Builder(zk, statusPath).claim().build();
     * Instead of claim you can also load the coordinate.
     */
    public static class Builder {
        private ZkStatusAndEndpoints.State state = ZkStatusAndEndpoints.State.UNKNOWN;
        private ZooKeeper zk = null;
        private String path = null;
        private int lastStatusVersion = -1000;
        private ServiceStatus serviceStatus = new ServiceStatus(ServiceState.UNASSIGNED,
                "No service state has been assigned");
        private Map endpointsByName = new HashMap();
        private ObjectMapper objectMapper = new ObjectMapper();

        /**
         * Constructor for builder.
         * @param zk The ZooKeeper instance to use.
         * @param path The serviceStatus/endpoints path of the coordinate to claim or load.
         */
        Builder(ZooKeeper zk, String path) {
            this.zk = zk;
            this.path = path;
        }

        /**
         * Returns the ZooKeeper.
         * @return ZooKeeper.
         */
        public ZooKeeper getZooKeeper() {
            return zk;
        }

        /**
         * Returns the path.
         * @return serviceStatus/endpoints path of the coordinate.
         */
        public String getPath() {
            return path;
        }

        /**
         * Returns the serviceStatus.
         * @return serviceStatus.
         */
        public ServiceStatus getServiceStatus() {
            return serviceStatus;
        /**
         * Returns the ObjectMapper instance.
         * @return ObjectMapper.
         */
        public ObjectMapper getObjectMapper() {
            return objectMapper;
        }

        /**
         * Returns the State.
         * @return state
         */
        public ZkStatusAndEndpoints.State getState() {
            return state;
        }

        /**
         * Returns the version if the coordinate was claimed.
         * @return version.
         */
        public int getLastStatusVersion() {
            return lastStatusVersion;
        }

        /**
         * Returns a list of Endpoints if loaded or an empty list of endpoints if claimed.
         * @return Map endpoints.
         */
        public Map getEndpointsByName() {
            return endpointsByName;
        }

        /**
         * Builds an instance of ZkStatusAndEndpoints. Requires that load() or claim() is called upfront.
         * @return this.
         */
        public ZkStatusAndEndpoints build() {
            if (state == ZkStatusAndEndpoints.State.UNKNOWN) {
                throw new IllegalStateException("Call load or claim before building.");
            }
            return new ZkStatusAndEndpoints(this);
        }

        /**
         * Loads the coordinate from ZooKeeper.
         * @return this.
         */
        public Builder load() {
            if (state != ZkStatusAndEndpoints.State.UNKNOWN) {
                throw new IllegalStateException("Does not make sense to load when something is already set.");
            }
            Stat stat = new Stat();
            try {
                byte[] data = zk.getData(path, false /*watcher*/, stat);

                serviceStatus = deserialize(new String(data, Util.CHARSET_NAME), objectMapper, endpointsByName);


            } catch (KeeperException e) {
                throw new CloudnameException(e);

            } catch (InterruptedException e) {
                throw new CloudnameException(e);

            } catch (UnsupportedEncodingException e) {
                throw new CloudnameException(e);
            }
            state = State.LOADED;

            return this;
        }

        /**
         * Claims a coordinate.
         * @return this.
         */
        public Builder claim() {
            if (state != State.UNKNOWN) {
                throw new IllegalStateException("Does not make sense to claim when something is already loaded.");
            }
            try {
                zk.create(
                        path, serialize(serviceStatus, endpointsByName).getBytes(Util.CHARSET_NAME),
                        ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            } catch (KeeperException.NodeExistsException e) {
                log.info("Coordinate already claimed  (" + path + ")");
                throw new CloudnameException.AlreadyClaimed(e);
            } catch (KeeperException.NoNodeException e) {
                log.info("Coordinate does not exist  (" + path + ")");
                throw new CloudnameException.CoordinateNotFound(e);
            } catch (KeeperException e) {
                throw new CloudnameException(e);
            } catch (InterruptedException e) {
                throw new CloudnameException(e);
            } catch (UnsupportedEncodingException e) {
                // This is not supposed to be happening since CHARSET_NAME
                // should always be "UTF-8".
                throw new CloudnameException(e);
            }

            // Stat the serviceStatus node so we have the version.  If later
            // we try to operate on the serviceStatus node and we do not have
            // the correct version this can mean that someone else has
            // been meddling with the serviceStatus node.  In which case we
            // must complain loudly.
            try {
                Stat stat = zk.exists(path, false);
                lastStatusVersion = stat.getVersion();
            } catch (KeeperException e) {
                throw new CloudnameException(e);
            } catch (InterruptedException e) {
                throw new CloudnameException(e);
            }
            state = State.CLAIMED;
            return this;
        }
    }
>>>>>>> 745a07ffec20e7bc0bcfb6ca418aa18a5812d257
}
Solution content
            throw new CloudnameException(e);
        }
    }
}
File
ZkStatusAndEndpoints.java
Developer's decision
Version 1
Kind of conflict
Class declaration
Comment
Chunk
Conflicting content
        cn.claim(c);
    }

<<<<<<< HEAD
    /**
     * A coordinate listener that stores events and calls a latch.
     */
    class UnitTestCoordinateListener implements CoordinateListener {

        final public List events = new ArrayList();

        final private CountDownLatch latch;

        UnitTestCoordinateListener(CountDownLatch latch) {
            this.latch = latch;
        }

        @Override
        public void onConfigEvent(Event event, String message) {
            events.add(event);
            latch.countDown();
        }
    }

    /**
     * Sets up a claimed coordinate and a connection listener.
     * @param connectedLatch
     * @return
     */
    private UnitTestCoordinateListener setUpListenerEnvironment(CountDownLatch connectedLatch) {
        Coordinate c = Coordinate.parse("1.service.user.cell");
        ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
        cn.createCoordinate(c);
        ServiceHandle handle = cn.claim(c);
        UnitTestCoordinateListener listener = new UnitTestCoordinateListener(connectedLatch);
        handle.registerCoordinateListener(listener);
        return listener;
    }

    @Test
    public void testCoordinateListenerInitialEvent() throws  Exception {
        final CountDownLatch connectedLatch = new CountDownLatch(1);
        UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
        assertTrue(connectedLatch.await(2, TimeUnit.SECONDS));
        assertEquals(1, listener.events.size());
        assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
    }

    @Test
    public void testCoordinateListenerConnectionDies() throws  Exception {

        final CountDownLatch connectedLatch = new CountDownLatch(2);
        UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
        log.info("Killing zookeeper");
        ezk.shutdown();
        assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
        assertEquals(2, listener.events.size());
        assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
        assertEquals(CoordinateListener.Event.LOST_CONNECTION_TO_STORAGE, listener.events.get(1));
    }

    @Test
    public void testCoordinateListenerCoordinateLost() throws  Exception {
        final CountDownLatch connectedLatch = new CountDownLatch(2);
        UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
        log.info("Deleting coordinate");
        zk.delete("/cn/cell/user/service/1/status", -1);
        assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
        assertEquals(2, listener.events.size());
        assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
        assertEquals(CoordinateListener.Event.COORDINATE_VANISHED, listener.events.get(1));
    }

    @Test
    public void testCoordinateListenerCoordinateCorrupted() throws  Exception {
        final CountDownLatch connectedLatch = new CountDownLatch(2);
        UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
        log.info("Corrupting coordinate.");
        String garbage = "sdfgsdfgsfgdsdfgsdfgsdfg";
        byte[] garbageBytes = garbage.getBytes("UTF-16LE");

        zk.setData("/cn/cell/user/service/1/status", garbageBytes, -1);
        assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
        assertEquals(2, listener.events.size());
        assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
        assertEquals(CoordinateListener.Event.COORDINATE_CORRUPTED, listener.events.get(1));
    }

    @Test
    public void testCoordinateListenerCoordinateOutOfSync() throws  Exception {
        final CountDownLatch connectedLatch = new CountDownLatch(2);
        UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);

        log.info("Writing different coordinate.");
        String source = "\"{\\\"state\\\":\\\"STARTING\\\",\\\"message\\\":\\\"Lost hamster.\\\"}\" {}";
        byte[] byteArray = source.getBytes(Util.CHARSET_NAME);

        zk.setData("/cn/cell/user/service/1/status", byteArray, -1);
        assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
        assertEquals(2, listener.events.size());
        assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
        assertEquals(CoordinateListener.Event.COORDINATE_OUT_OF_SYNC, listener.events.get(1));
    }
    
=======
    @Test
    public void testDestroyBasic() throws Exception {
        Coordinate c = Coordinate.parse("1.service.user.cell");
        ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
        cn.createCoordinate(c);
        assertTrue(pathExists("/cn/cell/user/service/1/config"));
        cn.destroyCoordinate(c);
        assertFalse(pathExists("/cn/cell/user/service"));
        assertTrue(pathExists("/cn/cell/user"));
    }

    @Test
    public void testDestroyTwoInstances() throws Exception {
        Coordinate c1 = Coordinate.parse("1.service.user.cell");
        Coordinate c2 = Coordinate.parse("2.service.user.cell");
        ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
        cn.createCoordinate(c1);
        cn.createCoordinate(c2);
        assertTrue(pathExists("/cn/cell/user/service/1/config"));
        assertTrue(pathExists("/cn/cell/user/service/2/config"));
        cn.destroyCoordinate(c1);
        assertFalse(pathExists("/cn/cell/user/service/1"));
        assertTrue(pathExists("/cn/cell/user/service/2/config"));
    }

    @Test (expected = CloudnameException.CoordinateIsClaimed.class)
    public void testDestroyClaimed() throws Exception {
        Coordinate c = Coordinate.parse("1.service.user.cell");
        ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
        cn.createCoordinate(c);
        ServiceHandle handle = cn.claim(c);
        cn.destroyCoordinate(c);
    }

>>>>>>> 745a07ffec20e7bc0bcfb6ca418aa18a5812d257
    private boolean pathExists(String path) throws Exception {
        return (null != zk.exists(path, false));
    }
Solution content
        cn.claim(c);
    }

    /**
     * A coordinate listener that stores events and calls a latch.
     */
    class UnitTestCoordinateListener implements CoordinateListener {

        final public List events = new ArrayList();

        final private CountDownLatch latch;

        UnitTestCoordinateListener(CountDownLatch latch) {
            this.latch = latch;
        }

        @Override
        public void onConfigEvent(Event event, String message) {
            events.add(event);
            latch.countDown();
        }
    }

    /**
     * Sets up a claimed coordinate and a connection listener.
     * @param connectedLatch
     * @return
     */
    private UnitTestCoordinateListener setUpListenerEnvironment(CountDownLatch connectedLatch) {
        Coordinate c = Coordinate.parse("1.service.user.cell");
        ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
        cn.createCoordinate(c);
        ServiceHandle handle = cn.claim(c);
        UnitTestCoordinateListener listener = new UnitTestCoordinateListener(connectedLatch);
        handle.registerCoordinateListener(listener);
        return listener;
    }

    @Test
    public void testCoordinateListenerInitialEvent() throws  Exception {
        final CountDownLatch connectedLatch = new CountDownLatch(1);
        UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
        assertTrue(connectedLatch.await(2, TimeUnit.SECONDS));
        assertEquals(1, listener.events.size());
        assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
    }

    @Test
    public void testCoordinateListenerConnectionDies() throws  Exception {

        final CountDownLatch connectedLatch = new CountDownLatch(2);
        UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
        log.info("Killing zookeeper");
        ezk.shutdown();
        assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
        assertEquals(2, listener.events.size());
        assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
        assertEquals(CoordinateListener.Event.LOST_CONNECTION_TO_STORAGE, listener.events.get(1));
    }

    @Test
    public void testCoordinateListenerCoordinateLost() throws  Exception {
        final CountDownLatch connectedLatch = new CountDownLatch(2);
        UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
        log.info("Deleting coordinate");
        zk.delete("/cn/cell/user/service/1/status", -1);
        assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
        assertEquals(2, listener.events.size());
        assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
        assertEquals(CoordinateListener.Event.COORDINATE_VANISHED, listener.events.get(1));
    }

    @Test
    public void testCoordinateListenerCoordinateCorrupted() throws  Exception {
        final CountDownLatch connectedLatch = new CountDownLatch(2);
        UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
        log.info("Corrupting coordinate.");
        String garbage = "sdfgsdfgsfgdsdfgsdfgsdfg";
        byte[] garbageBytes = garbage.getBytes("UTF-16LE");

        zk.setData("/cn/cell/user/service/1/status", garbageBytes, -1);
        assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
        assertEquals(2, listener.events.size());
        assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
        assertEquals(CoordinateListener.Event.COORDINATE_CORRUPTED, listener.events.get(1));
    }

    @Test
    public void testCoordinateListenerCoordinateOutOfSync() throws  Exception {
        final CountDownLatch connectedLatch = new CountDownLatch(2);
        UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);

        log.info("Writing different coordinate.");
        String source = "\"{\\\"state\\\":\\\"STARTING\\\",\\\"message\\\":\\\"Lost hamster.\\\"}\" {}";
        byte[] byteArray = source.getBytes(Util.CHARSET_NAME);

        zk.setData("/cn/cell/user/service/1/status", byteArray, -1);
        assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
        assertEquals(2, listener.events.size());
        assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
        assertEquals(CoordinateListener.Event.COORDINATE_OUT_OF_SYNC, listener.events.get(1));
    }

    @Test
    public void testDestroyBasic() throws Exception {
        Coordinate c = Coordinate.parse("1.service.user.cell");
        ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
        cn.createCoordinate(c);
        assertTrue(pathExists("/cn/cell/user/service/1/config"));
        cn.destroyCoordinate(c);
        assertFalse(pathExists("/cn/cell/user/service"));
        assertTrue(pathExists("/cn/cell/user"));
    }

    @Test
    public void testDestroyTwoInstances() throws Exception {
        Coordinate c1 = Coordinate.parse("1.service.user.cell");
        Coordinate c2 = Coordinate.parse("2.service.user.cell");
        ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
        cn.createCoordinate(c1);
        cn.createCoordinate(c2);
        assertTrue(pathExists("/cn/cell/user/service/1/config"));
        assertTrue(pathExists("/cn/cell/user/service/2/config"));
        cn.destroyCoordinate(c1);
        assertFalse(pathExists("/cn/cell/user/service/1"));
        assertTrue(pathExists("/cn/cell/user/service/2/config"));
    }

    @Test (expected = CloudnameException.CoordinateIsClaimed.class)
    public void testDestroyClaimed() throws Exception {
        Coordinate c = Coordinate.parse("1.service.user.cell");
        ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
        cn.createCoordinate(c);
        ServiceHandle handle = cn.claim(c);
        cn.destroyCoordinate(c);
    }

    private boolean pathExists(String path) throws Exception {
        return (null != zk.exists(path, false));
    }
File
ZkCloudnameTest.java
Developer's decision
Concatenation
Kind of conflict
Annotation
Class declaration
Comment
Method declaration