All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.db.util.groupcommit.GroupSizeFixWorker Maven / Gradle / Ivy

Go to download

A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases

The newest version!
package com.scalar.db.util.groupcommit;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import javax.annotation.concurrent.ThreadSafe;

// A worker manages NormalGroup instances to size-fix timed-out groups and pass them to
// DelayedSlotMoveWorker.
// Ready NormalGroup is passed to GroupCleanupWorker.
@ThreadSafe
class GroupSizeFixWorker
    extends BackgroundWorker<
        NormalGroup> {
  private final DelayedSlotMoveWorker<
          PARENT_KEY, CHILD_KEY, FULL_KEY, EMIT_PARENT_KEY, EMIT_FULL_KEY, V>
      delayedSlotMoveWorker;
  private final GroupCleanupWorker<
          PARENT_KEY, CHILD_KEY, FULL_KEY, EMIT_PARENT_KEY, EMIT_FULL_KEY, V>
      groupCleanupWorker;

  GroupSizeFixWorker(
      String label,
      long queueCheckIntervalInMillis,
      DelayedSlotMoveWorker
          delayedSlotMoveWorker,
      GroupCleanupWorker
          groupCleanupWorker) {
    super(
        label + "-group-commit-normal-group-size-fix",
        queueCheckIntervalInMillis,
        RetryMode.KEEP_AT_HEAD);
    this.delayedSlotMoveWorker = delayedSlotMoveWorker;
    this.groupCleanupWorker = groupCleanupWorker;
  }

  private void enqueueItemToNextQueue(
      NormalGroup normalGroup) {
    if (normalGroup.isReady()) {
      groupCleanupWorker.add(normalGroup);
    } else {
      delayedSlotMoveWorker.add(normalGroup);
    }
  }

  @Override
  BlockingQueue>
      createQueue() {
    // Use a normal queue because:
    // - Queued groups are removed once processed, without being re-enqueued
    // - No need for a priority queue since the order of queued groups is basically consistent with
    //   the timeout order
    return new LinkedBlockingQueue<>();
  }

  @Override
  boolean processItem(
      NormalGroup normalGroup) {
    // Size-fix the group if needed.
    if (normalGroup.isSizeFixed()) {
      enqueueItemToNextQueue(normalGroup);
      // It's already size-fixed. Should remove the item.
      return true;
    }

    long now = System.currentTimeMillis();
    if (normalGroup.groupSizeFixTimeoutAtMillis() < now) {
      // Expired. Fix the size.
      normalGroup.fixSize();

      enqueueItemToNextQueue(normalGroup);

      // Should remove the item.
      return true;
    }

    // Should not remove the item.
    return false;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy