Commit e10408e5 authored by chiaming2000's avatar chiaming2000
Browse files

Throws VersionMismatchException if PUT command received a

VERSION_MISMATCH status code from drive/simulator. 

Applications may now catch VersionMismatchException when performing PUT
operations.
parent 406663b3
Loading
Loading
Loading
Loading
+24 −21
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ package com.seagate.kinetic.client.internal;
import kinetic.client.Entry;
import kinetic.client.EntryMetadata;
import kinetic.client.KineticException;
import kinetic.client.VersionMismatchException;

import com.google.protobuf.ByteString;
import com.seagate.kinetic.common.lib.KineticMessage;
@@ -123,10 +124,9 @@ public class MessageFactory {
    public static void checkPutReply(KineticMessage reply,
            MessageType expectedType)
                    throws KineticException {

        // check error status
        checkErrorStatus(reply);

        /**
         * put response throws VersionMismatchException if received VERSION_MISMATCH status code.
         */
        if (reply.getMessage().getCommand().getHeader().getMessageType() != expectedType) {
            throw new KineticException("received wrong message type.");
        }
@@ -135,24 +135,27 @@ public class MessageFactory {
            throw new KineticException("no KV in response.");
        }

        // if (!reply.getCommand().hasStatus()) {
        // throw new KineticException("no KV.Status");
        // }
        if (!reply.getMessage().getCommand().hasStatus()) {
            throw new KineticException("no KV.Status");
        }

        // if (reply.getCommand().getStatus().getCode() ==
        // Status.StatusCode.VERSION_MISMATCH) {
        //
        // throw new KineticException("VersionException: "
        // + reply.getCommand().getStatus().getCode() + ": "
        // + reply.getCommand().getStatus().getStatusMessage());
        // }
        //
        // if (reply.getCommand().getStatus().getCode() !=
        // Status.StatusCode.SUCCESS) {
        // throw new KineticException("Unknown Error: "
        // + reply.getCommand().getStatus().getCode() + ": "
        // + reply.getCommand().getStatus().getStatusMessage());
        // }
        if (reply.getMessage().getCommand().getStatus().getCode() ==
            Status.StatusCode.VERSION_MISMATCH) {
        
            throw new VersionMismatchException("Kinetic Command Exception: "
                    + reply.getMessage().getCommand().getStatus().getCode()
                    + ": "
                    + reply.getMessage().getCommand().getStatus()
                            .getStatusMessage());
        }

        if (reply.getMessage().getCommand().getStatus().getCode() != Status.StatusCode.SUCCESS) {
            throw new KineticException("Kinetic Command Exception: "
                    + reply.getMessage().getCommand().getStatus().getCode()
                    + ": "
                    + reply.getMessage().getCommand().getStatus()
                    .getStatusMessage());
        }
    }

    public static boolean checkDeleteReply(KineticMessage reply)
+5 −2
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ public interface KineticClient extends GenericKineticClient {
	 * Put the specified <code>Entry</code> entry to the persistent store.
	 * Replace the version in the store with the new version. If the version in
	 * the specified entry does not match the version stored in the persistent
	 * store, a <code>KineticException</code> is thrown.
	 * store, a <code>VersionMismatchException</code> is thrown.
	 * <p>
	 * The specified entry is guaranteed to be persisted in the store if the
	 * call returns successfully.
@@ -108,6 +108,9 @@ public interface KineticClient extends GenericKineticClient {
	 * 
	 * @return a shallow copy of the specified entry but the value of version in
	 *         the entry metadata is set to the new version.
	 * 
	 * @throws VersionMismatchException 
	 *              If the version in the specified entry does not match the version stored.
     * 
	 * @throws KineticException
	 *             if any internal error occurred.
@@ -149,7 +152,7 @@ public interface KineticClient extends GenericKineticClient {
	/**
	 * Put the versioned <code>Entry</code> asynchronously. If the version in
	 * the specified entry does not match the version stored in the persistent
	 * store, a <code>KineticException</code> is delivered to the callback
	 * store, a <code>VersionMismatchException</code> is delivered to the callback
	 * instance.
	 * 
	 * @param entry
+50 −0
Original line number Diff line number Diff line
/**
 * Copyright (C) 2014 Seagate Technology.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package kinetic.client;

/**
 * 
 * This exception is thrown when a version mismatch occurred during a PUT operation.
 * 
 * @see KineticClient#put(Entry, byte[])
 * 
 * @author James Hughes
 * @author chiaming yang
 *
 */
public class VersionMismatchException extends KineticException {

    private static final long serialVersionUID = -6591860012767352506L;

    public VersionMismatchException() {
        ;
    }

    public VersionMismatchException(String message) {
        super(message);
    }

    public VersionMismatchException(Throwable cause) {
        super(cause);
    }

    public VersionMismatchException(String message, Throwable cause) {
        super(message, cause);
    }

}
+6 −4
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import kinetic.client.EntryMetadata;
import kinetic.client.KineticClient;
import kinetic.client.KineticClientFactory;
import kinetic.client.KineticException;
import kinetic.client.VersionMismatchException;
import kinetic.client.advanced.AdvancedKineticClient;
import kinetic.simulator.SimulatorConfiguration;

@@ -146,7 +147,7 @@ public class KineticBoundaryTest extends IntegrationTestCase {

            getClient().put(versioned, newVersion);
            fail("Should have thrown");
        } catch (KineticException e1) {
        } catch (VersionMismatchException e1) {
            Entry vGet = getClient().get(key);
            assertEntryEquals(key, valueInit, newVersionInit, vGet);
        }
@@ -183,9 +184,10 @@ public class KineticBoundaryTest extends IntegrationTestCase {
        try {
            getClient().put(newEntryWithInvalidDbVersion, newVersion);
            fail("Should have thrown");
        } catch (KineticException e1) {
            assertEquals("Kinetic Command Exception: VERSION_MISMATCH: ",
                    e1.getMessage());
        } catch (VersionMismatchException e1) {
            logger.info("caught expected VersionMismatchException exception.");
        } catch (KineticException ke) {
            fail ("Should have caught VersionMismatchException.");
        }

        logger.info(this.testEndInfo());