Commit 37e980fb authored by chiaming2000's avatar chiaming2000
Browse files

Simulator: Assign world wide name with an unique UUID for each instance

of simulator and persist the information under Kinetic home directory.
   
parent c649a52f
Loading
Loading
Loading
Loading
+11 −13
Original line number Diff line number Diff line
@@ -184,6 +184,11 @@ public class SimulatorEngine implements MessageService {
        // config for the current instance
        this.config = config;

        try {

            // calculate my home
            kineticHome = kineticHome(config);

            // heart beat
            if (config.getTickTime() > 0) {
                // construct new heart beat instance
@@ -193,14 +198,6 @@ public class SimulatorEngine implements MessageService {
            // register to use thread pool
            tpService.register(this);

        // p2p op handler.
        // p2pHandler = new P2POperationHandler();

        try {

            // calculate my home
            kineticHome = kineticHome(config);

            // load acl and pins
            SecurityHandler.loadACL(this);

@@ -217,7 +214,8 @@ public class SimulatorEngine implements MessageService {
            this.initHandlers();

            logger.info("simulator protocol version = "
                    + SimulatorConfiguration.getProtocolVersion());
                    + SimulatorConfiguration.getProtocolVersion() + ", wwn="
                    + config.getWorldWideName());

        } catch (Exception e) {
            e.printStackTrace();
+2 −1
Original line number Diff line number Diff line
@@ -123,6 +123,7 @@ public class KineticSimulator {
		KineticSimulator simulator = new KineticSimulator(serverConfig);

		logger.info("Kinetic simulator started, port: "
				+ simulator.getServerConfiguration().getPort());
                + simulator.getServerConfiguration().getPort() + ", WWN="
                + serverConfig.getWorldWideName());
	}
}
+72 −70
Original line number Diff line number Diff line
@@ -20,16 +20,19 @@
package kinetic.simulator;

import java.io.File;
import java.net.InetAddress;
import java.security.MessageDigest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.codec.binary.Hex;

import com.google.protobuf.ByteString;
import com.seagate.kinetic.proto.Kinetic;
import com.seagate.kinetic.proto.Kinetic.Command.GetLog.Configuration;
import com.seagate.kinetic.simulator.heartbeat.HeartbeatProvider;
import com.seagate.kinetic.simulator.heartbeat.provider.MulticastHeartbeatProvider;
import com.seagate.kinetic.simulator.internal.SimulatorEngine;

/**
 * 
@@ -242,15 +245,10 @@ public class SimulatorConfiguration extends Properties {
     */
    private HeartbeatProvider heartbeatProvider = null;

    /**
     * Serial number hash code
     */
    private int serialNumberHash = -1;

    /**
     * Serial number as a string
     */
    private String serialNumberString = null;
    private String serialNumber = null;

    /**
     * unique world wide name for each instance of simulator.
@@ -955,62 +953,94 @@ public class SimulatorConfiguration extends Properties {
    /**
     * Get the serial number of the running instance of simulator.
     * <p>
     * This number is to simulator a drive's serial number. The number is
     * obtained from the hash code of the following string:
     * <p>
     * ip + "kinetic home" + "persist home"
     * This number is to simulator a drive's serial number.
     * 
     * @return the serial number of the running instance of simulator
     */
    public String getSerialNumber() {

        if (this.serialNumberString == null) {
            this.calculateSerialNumber();
        if (this.worldWideName == null) {
            this.calculateWorldWideName();
        }

        return "S" + String.valueOf(serialNumberHash);
        return this.serialNumber;
    }

    private synchronized void calculateSerialNumber() {
    private synchronized void calculateWorldWideName() {

        if (this.serialNumberString != null) {
        if (this.worldWideName != null) {
            return;
        }

        String khome = getSimulatorHome();
        try {
            // calculate kinetic home
            String khome = SimulatorEngine.kineticHome(this);

        // get persist home name, use port# if not set
        String persistHome = getProperty(SimulatorConfiguration.PERSIST_HOME,
                String.valueOf(getPort()));
            // get wwn path
            String wwnFilePath = khome + File.separator + ".wwn";

        // int phomeHash = Math.abs(persistHome.hashCode());
            // wwn file instance
            File wwnFile = new File(wwnFilePath);

        // default ip of this instance
        String ip = "127.0.0.1";
            if (wwnFile.exists()) {

                /**
                 * The file exists, get wwn from the content.
                 */
                FileInputStream in = new FileInputStream(wwnFile);

                // read contents
                Configuration conf = Configuration.parseFrom(in);
                in.close();

                // get wwn ByteString
                ByteString wwn = conf.getWorldWideName();

                // get wwn Java String type
                this.worldWideName = wwn.toStringUtf8();

                // get serial number
                this.serialNumber = conf.getSerialNumber().toStringUtf8();
            } else {
                // get UUID for this instance
                UUID uuid = UUID.randomUUID();

                // wwn name
                this.worldWideName = uuid.toString();

                // calculate serial number.
                this.serialNumber = "S" + Math.abs(worldWideName.hashCode());

                /**
                 * persist wwn/serial number.
                 */
                FileOutputStream out = new FileOutputStream(wwnFile);
                Configuration.Builder cb = Configuration.newBuilder();

                // set serial number
                cb.setSerialNumber(ByteString.copyFromUtf8(this.serialNumber));

                // set wwn
                cb.setWorldWideName(ByteString.copyFromUtf8(this.worldWideName));

                // persist conf.
                cb.build().writeTo(out);

                out.close();
            }

        try {
            // get from Java API
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            this.worldWideName = "SIMULATOR-" + System.nanoTime();
            this.serialNumber = "S" + Math.abs(worldWideName.hashCode());
            logger.log(Level.WARNING, e.getMessage(), e);
        } finally {
            ;
        }

        // construct sn
        this.serialNumberString = ip + khome + persistHome;

        // calculate serial number hash code.
        serialNumberHash = Math.abs(serialNumberString.hashCode());
    }

    /**
     * Get world wide name of the running instance of the simulator.
     * <p>
     * The string is a hex of md5 obtained from the following components:
     * <p>
     * ip + "kinetic home" + "persist home"
     * <p>
     * Applications should handle MD5 collision for the name as desired if
     * necessary.
     *
     * @return world wide name of the running instance of the simulato
     */
@@ -1023,34 +1053,6 @@ public class SimulatorConfiguration extends Properties {
        return this.worldWideName;
    }

    private synchronized void calculateWorldWideName() {

        if (this.worldWideName != null) {
            return;
        }

        try {

            // calculate serial number
            this.getSerialNumber();

            // calculate md5
            MessageDigest md5 = MessageDigest.getInstance("MD5");

            // ip + kinetic home + persist home
            md5.update(this.serialNumberString.getBytes("UTF8"));

            byte[] digest = md5.digest();

            String digestHex = Hex.encodeHexString(digest);

            this.worldWideName = digestHex;

        } catch (Exception e) {
            logger.warning(e.getMessage());
        }
    }

    /**
     * Get maximum number of commands per batch request.
     * 
+8 −2
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@
 */
package com.seagate.kinetic.example.openstorage;

import java.io.File;

import kinetic.simulator.KineticSimulator;
import kinetic.simulator.SimulatorConfiguration;

@@ -68,9 +70,13 @@ public class VirtualDrives {
            config.setPort(myport);
            config.setSslPort(mySslPort);

            // set kinetic home for each drive
            String kineticHome = System.getProperty("user.home")
                    + File.separator + "kinetic" + File.separator + "instance_"
                    + myport;

            // set persist store home folder for each instance
            config.put(SimulatorConfiguration.PERSIST_HOME, "instance_"
                    + myport);
            config.put(SimulatorConfiguration.KINETIC_HOME, kineticHome);

            // start the simulator instance
            simulators[i] = new KineticSimulator(config);