Ejemplo n.º 1
0
 /**
  * disconnect from server
  *
  * <p>kills all thread related to the schema
  */
 public void close() {
   active = false;
   try {
     interrupt();
   } catch (Exception e) {
     // do nothing interrupting sockets
   }
   commandQueue.close();
 }
Ejemplo n.º 2
0
 /**
  * This method runs in the background
  *
  * <p>It waits for new command to be pushed into it,
  *
  * <p>and then pop the commands by their version number
  *
  * <p>in order to execute them
  */
 @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
 public void run() {
   Command modelCommand = null;
   while (active) {
     try {
       modelCommand = commandQueue.popCommand();
       if (modelCommand != null) {
         doJobs(modelCommand);
       }
     } catch (Exception e) {
       if (active) {
         log.fatal("An exception occur in the core thread : Expected lost of data ", e);
         modelCommand.push(COMMAND_STATUS, INTEGER, CommandStatus.unExpectedException.getId());
         synchronized (modelCommand) {
           modelCommand.notify();
         }
       }
     }
   }
 }
Ejemplo n.º 3
0
  /**
   * this is the heart of the storage,
   *
   * <p>It control all the in coming commands.
   *
   * <p>The method does the following
   *
   * <p>1. validate the command
   *
   * <p>2. update the version number and the conflict number (if needed)
   *
   * <p>3. push the command to the command queue
   *
   * <p>
   */
  public void consume(Command command) throws PlanckDBException {
    // update schema and coreManagerKey in command in case that the schema or coreManagerKey fields
    // in the command are null

    if (command.getSchemaId() < 0) {
      command.setSchemaId(getSessionMetaData().getSchemaId());
    }
    if (command.getCoreManagerKey() == null) {
      command.setCoreManagerKey(getCoreManager().getKey());
    }

    // you do not have to handle your messages which NetworkProtocolType is multicast
    // because you have already handle them
    final NetworkProtocolType type = command.getNetworkProtocolType();
    if (type != null
        && type.isCast()
        && command.getCoreManagerKey().equals(getCoreManager().getKey())
        && sessionMetaData.getSessionId() == command.getSessionId()) {
      return;
    }
    // TODO validate message

    if (command.isModeCommand() || command.isAtomicModelCommand()) { // model change commands

      // set version number or distribute
      if (command.getVersion() < 0) {
        distributionManager.produceTcp(command);
        int version = command.getVersion();
        // return if command is lock or something was wrong
        if (version < 0 || command.isNotSucceed()) {
          return;
        }
      }
      if (command.isAtomicModelCommand()) {
        List<Command> commands = command.getCommands();
        atomicContainer.register(command, commands);
        for (Command newCommand : commands) {
          commandQueue.pushCommand(newCommand);
        }
      } else {
        commandQueue.pushCommand(command);
      }
    } else {
      Integer commandType = command.getCommandType();
      if (commandType == READ_LOCK_COMMAND) {
        if (!command.getCoreManagerKey().equals(coreManager.getKey())) {
          List<Command> commands = command.getCommands();
          boolean lock = command.isLocked();
          for (Command newCommand : commands) {
            int entityId = newCommand.getEntityId();
            int ownerId = newCommand.getOwnerId();
            if (lock) {
              registry.lockEntity(entityId, true, ownerId);
            } else {
              registry.lockEntity(entityId, false, NON_ENTITY_OWNER);
            }
          }
        } else {
          distributionManager.produceTcp(command);
        }
      }
    }
  }