Commit 86976e8a authored by chiaming2000's avatar chiaming2000
Browse files

1. Simulator and Java API: Added logic to handle connection Id

verification/assignment when there are other exceptions also being
raised in the request message.  Such as cluster version mismatch
scenario.

2. Simulator: by default, the connection Id is checked and logged as
warning messages if does not match. The flag wil be reset when drive
code with this feature is released and enforced.

3. Simulator: uses "next available unique timestamp" (type long) for
connection Ids to be more consistent with the drive behavior.  
parent 072547d9
Loading
Loading
Loading
Loading
+19 −2
Original line number Diff line number Diff line
@@ -951,6 +951,8 @@ public class DefaultKineticClient implements AdvancedKineticClient {
        
        KineticMessage response = null;
        
        KineticException ke = null;
        
        try {
            // create get request message
            KineticMessage request = MessageFactory.createNoOpRequestMessage();
@@ -958,13 +960,28 @@ public class DefaultKineticClient implements AdvancedKineticClient {
            // send request
            response = this.client.request(request); 
        } catch (KineticException e) {
            // assign exception caught so that we can use it in the finally clause 
            ke = e;
            LOG.warning(e.getMessage());
        } finally {   
            if (response != null) {
                // set connectionId
                /**
                 * if got normal response, check and set connection Id.
                 */
                this.client.setConnectionId(response);
            } else if (ke != null){
                /**
                 * check if response message is set in the exception
                 */
                response = ke.getResponseMessage();
                if (response != null) {
                    /**
                     * set connection Id if necessary.
                     */
                    this.client.setConnectionId(response);
                }
            }
        }
    }

}
+20 −6
Original line number Diff line number Diff line
@@ -147,8 +147,8 @@ public class SimulatorEngine implements MessageService {
    //connection map
    private static ConcurrentHashMap<Object, ConnectionInfo> connectionMap = new ConcurrentHashMap<Object, ConnectionInfo>();
    
    // next connection id
    private static long nextConnectionId = 0;
    // last connection Id. 
    private static long lastConnectionId = System.currentTimeMillis();
    
    static {
        // add shutdown hook to clean up resources
@@ -768,12 +768,26 @@ public class SimulatorEngine implements MessageService {
    }
    
    /**
     * Get next available connection id.
     * Get next available unique connection id based on timestamp. The Id is guarantees to be unique for simulators 
     * running within the same JVM.
     * 
     * @return next available connection id in sequence.
     * @return next available unique connection ID based on timestamp.
     */
    private static synchronized long getNextConnectionId() {
        return nextConnectionId ++;
        
        // current time
        long id = System.currentTimeMillis();
        
        // check if duplicate.  enforce so that it is later than the time that this JVM is started.
        if (id <= lastConnectionId) {
            // increase one so its unique.
            id = lastConnectionId + 1;
        }
        
        // set last connection id
        lastConnectionId = id;
        
        return id;
    }
    
}
+16 −2
Original line number Diff line number Diff line
@@ -21,10 +21,13 @@ package com.seagate.kinetic.simulator.io.provider.nio;

import java.util.logging.Logger;

import kinetic.simulator.SimulatorConfiguration;

import com.seagate.kinetic.common.lib.KineticMessage;
import com.seagate.kinetic.simulator.internal.ConnectionInfo;
import com.seagate.kinetic.simulator.internal.SimulatorEngine;
import com.seagate.kinetic.simulator.internal.StatefulMessage;

import io.netty.channel.ChannelHandlerContext;

/**
@@ -46,8 +49,11 @@ public class NioConnectionStateManager {
     * 
     * @return A stateful message that contains a connection Id to be set in the response message.  
     *          Otherwise return null if connection Id has already set in a response message for this connection.
     *          
     * @throws RuntimeException if connection has already set by the simulator but received a connection Id 
     *         does not match. 
     */
    public static StatefulMessage getStatefulMessage(ChannelHandlerContext ctx,
    public static StatefulMessage checkAndGetStatefulMessage(ChannelHandlerContext ctx,
            KineticMessage request) {

        // get connection info for this channel
@@ -79,6 +85,14 @@ public class NioConnectionStateManager {
                            + ", received request message connection Id="
                            + request.getMessage().getCommand().getHeader()
                                    .getConnectionID());
                    
                    if (SimulatorConfiguration.getIsConnectionIdCheckEnforced()) {
                        throw new RuntimeException("expect CID="
                                + cinfo.getConnectionId()
                                + " , but received CID="
                                + request.getMessage().getCommand().getHeader()
                                        .getConnectionID());
                    }
               }
            }
        }
+1 −1
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ public class SslMessageServiceHandler extends
			KineticMessage request)
			throws Exception {
	    
	    StatefulMessage sm = NioConnectionStateManager.getStatefulMessage(ctx, request);
	    StatefulMessage sm = NioConnectionStateManager.checkAndGetStatefulMessage(ctx, request);

		if (enforceOrdering) {
		    // process request sequentially
+1 −1
Original line number Diff line number Diff line
@@ -86,7 +86,7 @@ public class NioMessageServiceHandler extends
					"Fault injected for the simulator");
		}
		
		StatefulMessage sm = NioConnectionStateManager.getStatefulMessage(ctx, request);
		StatefulMessage sm = NioConnectionStateManager.checkAndGetStatefulMessage(ctx, request);

		if (enforceOrdering) {
			// process request sequentially
Loading