public List<KeyValue> stream(Entity<?> entity) {
    List<KeyValue> streamed = new ArrayList<KeyValue>();

    byte[] keyBytes = Serialization.serialize(entity.getKey());
    byte[] qualifierBytes = new byte[0];

    long ts = System.currentTimeMillis();
    for (FieldKey<?> key : this.keys) {
      String name = key.getName();

      if (entity.isDirty(key)) {
        if (entity.getDirty(key) != null) {
          byte[] familyBytes = Serialization.serialize(key.getName());
          byte[] valueBytes =
              this.streamers.get(name).write(entity.get(key), Serialization.serialize(name), ts);
          KeyValue kv = new KeyValue(keyBytes, familyBytes, qualifierBytes, valueBytes);
          streamed.add(kv);
        } else {
          ErrorControl.fatal("Caught unsupported null dirty value for key: " + name);
        }
      }
    }

    return streamed;
  }
  public HBaseEntityStreamer(
      EntityService entityService,
      Class<T> entityType,
      List<FieldKey<?>> keys,
      PrimaryKey<?, ?> primaryKey) {
    this.entityService = entityService;
    this.entityType = entityType;

    this.keys = keys;

    this.streamers = new HashMap<String, HBaseComponentStreamer<?>>();
    for (FieldKey<?> key : this.keys) {
      HBaseComponentStreamer<?> streamer = generateStreamer(key);
      this.streamers.put(key.getName(), streamer);
    }

    HBaseComponentStreamer<?> tmpPrimaryStreamer = generateStreamer(primaryKey);
    if (!HBasePrimaryKeyStreamer.class.isAssignableFrom(tmpPrimaryStreamer.getClass())) {
      ErrorControl.fatal(
          String.format(
              "Using illegal primary key: %s for: %s",
              primaryKey.getType().getSimpleName(), entityType.getSimpleName()));
    } else {
      this.primaryStreamer = (HBasePrimaryKeyStreamer<?>) tmpPrimaryStreamer;
    }
  }
Beispiel #3
0
 public void close() {
   try {
     this.pool.removeHandler(this);
     socket.close();
   } catch (Exception e) {
     ErrorControl.logException(e);
   }
 }
Beispiel #4
0
  private SocketWorker create() {
    SocketWorker instance = null;

    try {
      instance = new SocketWorker(this, this.protocol);
    } catch (Exception e) {
      ErrorControl.logException(e);
    }

    return instance;
  }
Beispiel #5
0
 public void addResponse(SocketHandler handler, Future<byte[]> response) {
   if (response.isDone()) {
     try {
       ArrayBlockingQueue<byte[]> mailbox = this.mailboxes.get(handler);
       mailbox.add(response.get());
     } catch (Exception e) {
       ErrorControl.logException(e);
     }
   } else {
     this.futures.add(new Pair<SocketHandler, Future<byte[]>>(handler, response));
   }
 }
Beispiel #6
0
  public SocketHandler(Socket socket, SocketWorkerPool pool, ClusterProtocol protocol) {
    this.socket = socket;
    this.pool = pool;
    this.protocol = protocol;

    this.inbytes = new byte[IO_SIZE];

    this.responses = new ArrayBlockingQueue<byte[]>(QUEUE_SIZE);
    this.pool.addHandler(this, this.responses);

    try {
      this.input = new BufferedInputStream(socket.getInputStream());
      this.output = new BufferedOutputStream(socket.getOutputStream());
    } catch (Exception e) {
      ErrorControl.logException(e);
    }
  }
  @SuppressWarnings("unchecked")
  private HBaseComponentStreamer<?> generateStreamer(Key<?> key) {
    HBaseComponentStreamer<?> instance = null;

    try {
      Class<?> type = key.getType();
      ParameterizedType subType = key.getSubType();

      Class<? extends HBaseComponentStreamer<?>> cls =
          this.entityService.getStreamer(HBaseConnection.class, HBaseComponentStreamer.class, type);
      Constructor<? extends HBaseComponentStreamer<?>> ctor =
          cls.getConstructor(EntityService.class, Class.class, ParameterizedType.class);
      instance = ctor.newInstance(this.entityService, type, subType);
    } catch (Exception e) {
      ErrorControl.logException(e);
    }

    return instance;
  }
Beispiel #8
0
  public boolean handle() {
    boolean doneWork = false;

    try {
      int available = this.input.available();
      while (available > 0) {
        if (this.expected < 0 && available >= 4) {
          this.expected = this.input.read();
          available -= 4;
        } else {
          break;
        }

        if (this.expected > 0) {
          this.inputOffset +=
              this.input.read(this.inbytes, this.inputOffset, this.expected - this.inputOffset);
        }

        if (this.inputOffset == this.expected) {
          byte[] request = new byte[this.expected];
          System.arraycopy(this.inbytes, 0, request, 0, this.expected);
          this.pool.addRequest(this, request);
        }

        available = this.input.available();
        doneWork = true;
      }

      while (!this.responses.isEmpty()) {
        byte[] response = this.responses.poll();
        this.output.write(response.length);
        this.output.write(response);
        doneWork = true;
      }

      this.output.flush();
    } catch (Exception e) {
      ErrorControl.logException(e);
    }

    return doneWork;
  }
  @SuppressWarnings("unchecked")
  private void setupLayers() {
    Iterator<Class<?>> itr = new ClassWalker(ClassWalkerFilter.extending(ClusterLayer.class));

    while (itr.hasNext()) {
      Class<? extends ClusterLayer> cls = (Class<? extends ClusterLayer>) itr.next();

      this.logger.info(String.format("Found cluster layer: %s", cls.getName()));

      try {
        ClusterLayer instance = cls.newInstance();
        this.layerByClass.put(cls, instance);
      } catch (Exception e) {
        ErrorControl.logException(e);
      }
    }

    Class<? extends ClusterLayer>[] orderedLayers =
        (Class<? extends ClusterLayer>[])
            this.layerByClass.keySet().toArray(new Class<?>[this.layerByClass.size()]);
    Arrays.sort(
        orderedLayers,
        new Comparator<Class<? extends ClusterLayer>>() {

          @Override
          public int compare(
              Class<? extends ClusterLayer> arg0, Class<? extends ClusterLayer> arg1) {
            return arg0.getName().compareTo(arg1.getName());
          }
        });

    for (int i = 0; i < orderedLayers.length; i++) {
      Class<? extends ClusterLayer> layer = orderedLayers[i];
      this.ordinalByClass.put(layer, i);
      this.classByOrdinal.put(i, layer);
      this.layerByOrdinal.put(i, this.layerByClass.get(layer));
    }
  }