Ejemplo n.º 1
0
 public void resendMessage(long msgId) {
   for (SchedullerPackage schedullerPackage :
       messages.values().toArray(new SchedullerPackage[0])) {
     if (schedullerPackage.relatedMessageIds.contains(msgId)) {
       // schedullerPackage.relatedMessageIds.clear();
       schedullerPackage.state = STATE_QUEUED;
       schedullerPackage.lastAttemptTime = 0;
     }
   }
 }
Ejemplo n.º 2
0
  public synchronized PreparedPackage doSchedule(int contextId, boolean isInited) {
    ArrayList<SchedullerPackage> foundedPackages = actualPackages(contextId);

    synchronized (confirmedMessages) {
      if (foundedPackages.size() == 0
          && (confirmedMessages.size() <= MAX_ACK_COUNT
              || (System.nanoTime() - firstConfirmTime) < CONFIRM_TIMEOUT)) {
        return null;
      }
    }

    boolean useHighPriority = false;

    for (SchedullerPackage p : foundedPackages) {
      if (p.priority == PRIORITY_HIGH) {
        useHighPriority = true;
        break;
      }
    }

    ArrayList<SchedullerPackage> packages = new ArrayList<SchedullerPackage>();

    if (useHighPriority) {
      Logger.d("Scheduller", "Using high priority scheduling");
      int totalSize = 0;
      for (SchedullerPackage p : foundedPackages) {
        if (p.priority == PRIORITY_HIGH) {
          packages.add(p);
          totalSize += p.serialized.length;
          if (totalSize > MAX_WORKLOAD_SIZE) {
            break;
          }
        }
      }
    } else {
      int totalSize = 0;
      for (SchedullerPackage p : foundedPackages) {
        packages.add(p);
        Logger.d(
            "Scheduller", "Prepare package: " + p.supportTag + " of size " + p.serialized.length);
        totalSize += p.serialized.length;
        Logger.d("Scheduller", "Total size: " + totalSize);
        if (totalSize > MAX_WORKLOAD_SIZE) {
          break;
        }
      }
    }

    Logger.d(TAG, "Iteration: count: " + packages.size() + ", confirm:" + confirmedMessages.size());
    Logger.d(TAG, "Building package");
    if (foundedPackages.size() == 0 && confirmedMessages.size() != 0) {
      Long[] msgIds;
      synchronized (confirmedMessages) {
        msgIds = confirmedMessages.toArray(new Long[confirmedMessages.size()]);
        confirmedMessages.clear();
      }
      MTMsgsAck ack = new MTMsgsAck(msgIds);
      Logger.d(TAG, "Single msg_ack");
      try {
        return new PreparedPackage(
            generateSeqNoWeak(), generateMessageId(), ack.serialize(), useHighPriority);
      } catch (IOException e) {
        Logger.e(TAG, e);
        return null;
      }
    } else if (foundedPackages.size() == 1 && confirmedMessages.size() == 0) {
      SchedullerPackage schedullerPackage = foundedPackages.get(0);
      schedullerPackage.state = STATE_SENT;
      if (schedullerPackage.idGenerationTime == 0) {
        generateParams(schedullerPackage);
      }
      Logger.d(
          TAG,
          "Single package: #"
              + schedullerPackage.id
              + " "
              + schedullerPackage.supportTag
              + " ("
              + schedullerPackage.messageId
              + ", "
              + schedullerPackage.seqNo
              + ")");
      schedullerPackage.writtenToChannel = contextId;
      schedullerPackage.lastAttemptTime = getCurrentTime();
      return new PreparedPackage(
          schedullerPackage.seqNo,
          schedullerPackage.messageId,
          schedullerPackage.serialized,
          useHighPriority);
    } else {
      MTMessagesContainer container = new MTMessagesContainer();
      if ((confirmedMessages.size() > 0 && !useHighPriority) || (!isInited)) {
        try {
          Long[] msgIds;
          synchronized (confirmedMessages) {
            msgIds = confirmedMessages.toArray(new Long[0]);
            confirmedMessages.clear();
          }
          MTMsgsAck ack = new MTMsgsAck(msgIds);
          Logger.d(TAG, "Adding msg_ack: " + msgIds.length);
          container
              .getMessages()
              .add(new MTMessage(generateMessageId(), generateSeqNoWeak(), ack.serialize()));
        } catch (IOException e) {
          Logger.e(TAG, e);
        }
      }
      for (SchedullerPackage schedullerPackage : packages) {
        schedullerPackage.state = STATE_SENT;
        if (schedullerPackage.idGenerationTime == 0) {
          generateParams(schedullerPackage);
        }

        if (schedullerPackage.isDepend) {
          if (schedullerPackage.dependMessageId == 0) {
            if (lastDependId > 0) {
              schedullerPackage.dependMessageId = lastDependId;
            } else {
              schedullerPackage.dependMessageId = -1;
            }
          }

          lastDependId = schedullerPackage.messageId;
        }
        schedullerPackage.writtenToChannel = contextId;
        schedullerPackage.lastAttemptTime = getCurrentTime();
        if (schedullerPackage.isDepend && schedullerPackage.dependMessageId > 0) {

          Logger.d(
              TAG,
              "Adding package: #"
                  + schedullerPackage.id
                  + " "
                  + schedullerPackage.supportTag
                  + " ("
                  + schedullerPackage.messageId
                  + " on "
                  + schedullerPackage.dependMessageId
                  + ", "
                  + schedullerPackage.seqNo
                  + ")");

          MTInvokeAfter invokeAfter =
              new MTInvokeAfter(schedullerPackage.dependMessageId, schedullerPackage.serialized);
          try {
            container
                .getMessages()
                .add(
                    new MTMessage(
                        schedullerPackage.messageId,
                        schedullerPackage.seqNo,
                        invokeAfter.serialize()));
          } catch (IOException e) {
            Logger.e(TAG, e);
            // Never happens
          }
        } else {
          Logger.d(
              TAG,
              "Adding package: #"
                  + schedullerPackage.id
                  + " "
                  + schedullerPackage.supportTag
                  + " ("
                  + schedullerPackage.messageId
                  + ", "
                  + schedullerPackage.seqNo
                  + ")");
          container
              .getMessages()
              .add(
                  new MTMessage(
                      schedullerPackage.messageId,
                      schedullerPackage.seqNo,
                      schedullerPackage.serialized));
        }
      }

      long containerMessageId = generateMessageId();
      int containerSeq = generateSeqNoWeak();

      for (SchedullerPackage schedullerPackage : packages) {
        schedullerPackage.relatedMessageIds.add(containerMessageId);
      }

      Logger.d(TAG, "Sending Package (" + containerMessageId + ", " + containerSeq + ")");

      try {
        return new PreparedPackage(
            containerSeq, containerMessageId, container.serialize(), useHighPriority);
      } catch (IOException e) {
        // Might not happens
        Logger.e(TAG, e);
        return null;
      }
    }
  }