Java Code Examples for org.apache.helix.PropertyKey#getType()

The following examples show how to use org.apache.helix.PropertyKey#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends HelixProperty> boolean updateProperty(PropertyKey key, DataUpdater<ZNRecord> updater, T value) {
  PropertyType type = key.getType();
  String path = key.getPath();
  int options = constructOptions(type);

  boolean success = false;
  switch (type) {
  case CURRENTSTATES:
    success = _groupCommit.commit(_baseDataAccessor, options, path, value.getRecord(), true);
    break;
  case STATUSUPDATES:
    if (LOG.isTraceEnabled()) {
      LOG.trace("Update status. path: " + key.getPath() + ", record: " + value.getRecord());
    }
    break;
  default:
    success = _baseDataAccessor.update(path, updater, options);
    break;
  }
  return success;
}
 
Example 2
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public HelixProperty.Stat getPropertyStat(PropertyKey key) {
  PropertyType type = key.getType();
  String path = key.getPath();
  int options = constructOptions(type);
  try {
    Stat stat = _baseDataAccessor.getStat(path, options);
    if (stat != null) {
      return new HelixProperty.Stat(stat.getVersion(), stat.getCtime(), stat.getMtime(), stat.getEphemeralOwner());
    }
  } catch (ZkNoNodeException e) {

  }

  return null;
}
 
Example 3
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends HelixProperty> boolean[] createChildren(List<PropertyKey> keys, List<T> children) {
  // TODO: add validation
  int options = -1;
  List<String> paths = new ArrayList<String>();
  List<ZNRecord> records = new ArrayList<ZNRecord>();
  for (int i = 0; i < keys.size(); i++) {
    PropertyKey key = keys.get(i);
    PropertyType type = key.getType();
    String path = key.getPath();
    paths.add(path);
    HelixProperty value = children.get(i);
    records.add(value.getRecord());
    options = constructOptions(type);
  }
  return _baseDataAccessor.createChildren(paths, records, options);
}
 
Example 4
Source File: TestDefaultMessagingService.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends HelixProperty> List<T> getChildValues(PropertyKey key,
    boolean throwException) {
  PropertyType type = key.getType();
  List<T> result = new ArrayList<T>();
  Class<? extends HelixProperty> clazz = key.getTypeClass();
  if (type == PropertyType.EXTERNALVIEW || type == PropertyType.IDEALSTATES) {
    HelixProperty typedInstance = HelixProperty.convertToTypedInstance(clazz, _externalView);
    result.add((T) typedInstance);
    return result;
  } else if (type == PropertyType.LIVEINSTANCES) {
    return (List<T>) HelixProperty.convertToTypedList(clazz, _liveInstances);
  }

  return result;
}
 
Example 5
Source File: ZKHelixManager.java    From helix with Apache License 2.0 5 votes vote down vote up
void addListener(Object listener, PropertyKey propertyKey, ChangeType changeType,
    EventType[] eventType) {
  checkConnected(_waitForConnectedTimeout);

  PropertyType type = propertyKey.getType();

  synchronized (this) {
    for (CallbackHandler handler : _handlers) {
      // compare property-key path and listener reference
      if (handler.getPath().equals(propertyKey.getPath())
          && handler.getListener().equals(listener)) {
        LOG.info("Listener: " + listener + " on path: " + propertyKey.getPath()
            + " already exists. skip add");

        return;
      }
    }

    CallbackHandler newHandler =
        new CallbackHandler(this, _zkclient, propertyKey, listener, eventType, changeType,
            _callbackMonitors.get(changeType));

    _handlers.add(newHandler);
    LOG.info("Added listener: " + listener + " for type: " + type + " to path: "
        + newHandler.getPath());
  }
}
 
Example 6
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean removeProperty(PropertyKey key) {
  PropertyType type = key.getType();
  String path = key.getPath();
  int options = constructOptions(type);

  return _baseDataAccessor.remove(path, options);
}
 
Example 7
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> getChildNames(PropertyKey key) {
  PropertyType type = key.getType();
  String parentPath = key.getPath();
  int options = constructOptions(type);
  List<String> childNames = _baseDataAccessor.getChildNames(parentPath, options);
  if (childNames == null) {
    childNames = Collections.emptyList();
  }
  return childNames;
}
 
Example 8
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends HelixProperty> Map<String, T> getChildValuesMap(PropertyKey key,
    boolean throwException) {
  PropertyType type = key.getType();
  String parentPath = key.getPath();
  int options = constructOptions(type);
  List<T> children = getChildValues(key, throwException);
  Map<String, T> childValuesMap = new HashMap<String, T>();
  for (T t : children) {
    childValuesMap.put(t.getRecord().getId(), t);
  }
  return childValuesMap;
}
 
Example 9
Source File: MockZkHelixDataAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
private void addCount(PropertyKey key, int count) {
  PropertyType type = key.getType();
  if (!_readPathCounters.containsKey(type)) {
    _readPathCounters.put(type, 0);
  }
  _readPathCounters.put(type, _readPathCounters.get(type) + count);
}
 
Example 10
Source File: TestDefaultMessagingService.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends HelixProperty> T getProperty(PropertyKey key) {

  PropertyType type = key.getType();
  if (type == PropertyType.EXTERNALVIEW || type == PropertyType.IDEALSTATES) {
    return (T) new ExternalView(_externalView);
  }
  return null;
}
 
Example 11
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends HelixProperty> boolean setProperty(PropertyKey key, T value) {
  PropertyType type = key.getType();
  if (!value.isValid()) {
    throw new HelixMetaDataAccessException("The ZNRecord for " + type + " is not valid.");
  }

  String path = key.getPath();
  int options = constructOptions(type);

  boolean success = false;
  switch (type) {
  case IDEALSTATES:
  case EXTERNALVIEW:
    // check if bucketized
    if (value.getBucketSize() > 0) {
      // set parent node
      ZNRecord metaRecord = new ZNRecord(value.getId());
      metaRecord.setSimpleFields(value.getRecord().getSimpleFields());
      success = _baseDataAccessor.set(path, metaRecord, options);
      if (success) {
        ZNRecordBucketizer bucketizer = new ZNRecordBucketizer(value.getBucketSize());

        Map<String, ZNRecord> map = bucketizer.bucketize(value.getRecord());
        List<String> paths = new ArrayList<String>();
        List<ZNRecord> bucketizedRecords = new ArrayList<ZNRecord>();
        for (String bucketName : map.keySet()) {
          paths.add(path + "/" + bucketName);
          bucketizedRecords.add(map.get(bucketName));
        }

        // TODO: set success accordingly
        _baseDataAccessor.setChildren(paths, bucketizedRecords, options);
      }
    } else {
      success = _baseDataAccessor.set(path, value.getRecord(), options);
    }
    break;
  default:
    success = _baseDataAccessor.set(path, value.getRecord(), options);
    break;
  }
  return success;
}
 
Example 12
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends HelixProperty> T getProperty(PropertyKey key) {
  PropertyType type = key.getType();
  String path = key.getPath();
  int options = constructOptions(type);
  ZNRecord record = null;
  try {
    Stat stat = new Stat();
    record = _baseDataAccessor.get(path, stat, options);
    if (record != null) {
      record.setCreationTime(stat.getCtime());
      record.setModifiedTime(stat.getMtime());
      record.setVersion(stat.getVersion());
      record.setEphemeralOwner(stat.getEphemeralOwner());
    }
  } catch (ZkNoNodeException e) {
    // OK
  }

  switch (type) {
  case CURRENTSTATES:
  case IDEALSTATES:
  case EXTERNALVIEW:
    // check if bucketized
    if (record != null) {
      HelixProperty property = new HelixProperty(record);

      int bucketSize = property.getBucketSize();
      if (bucketSize > 0) {
        // @see HELIX-574
        // clean up list and map fields in case we write to parent node by mistake
        property.getRecord().getMapFields().clear();
        property.getRecord().getListFields().clear();

        List<ZNRecord> childRecords = _baseDataAccessor.getChildren(path, null, options, 0, 0);
        ZNRecord assembledRecord = new ZNRecordAssembler().assemble(childRecords);

        // merge with parent node value
        if (assembledRecord != null) {
          record.getSimpleFields().putAll(assembledRecord.getSimpleFields());
          record.getListFields().putAll(assembledRecord.getListFields());
          record.getMapFields().putAll(assembledRecord.getMapFields());
        }
      }
    }
    break;
  default:
    break;
  }

  @SuppressWarnings("unchecked")
  T t = (T) HelixProperty.convertToTypedInstance(key.getTypeClass(), record);
  return t;
}
 
Example 13
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends HelixProperty> List<T> getChildValues(PropertyKey key, boolean throwException) {
  PropertyType type = key.getType();
  String parentPath = key.getPath();
  int options = constructOptions(type);
  List<T> childValues = new ArrayList<T>();
  List<ZNRecord> children;
  if (throwException) {
    children = _baseDataAccessor.getChildren(parentPath, null, options, 1, 0);
  } else {
    children = _baseDataAccessor.getChildren(parentPath, null, options);
  }
  if (children != null) {
    for (ZNRecord record : children) {
      switch (type) {
      case CURRENTSTATES:
      case IDEALSTATES:
      case EXTERNALVIEW:
        if (record != null) {
          HelixProperty property = new HelixProperty(record);

          int bucketSize = property.getBucketSize();
          if (bucketSize > 0) {
            // TODO: fix this if record.id != pathName
            String childPath = parentPath + "/" + record.getId();
            List<ZNRecord> childRecords;
            if (throwException) {
              childRecords = _baseDataAccessor.getChildren(childPath, null, options, 1, 0);
            } else {
              childRecords = _baseDataAccessor.getChildren(childPath, null, options);
            }
            ZNRecord assembledRecord = new ZNRecordAssembler().assemble(childRecords);

            // merge with parent node value
            if (assembledRecord != null) {
              record.getSimpleFields().putAll(assembledRecord.getSimpleFields());
              record.getListFields().putAll(assembledRecord.getListFields());
              record.getMapFields().putAll(assembledRecord.getMapFields());
            }
          }
        }

        break;
      default:
        break;
      }

      if (record != null) {
        @SuppressWarnings("unchecked")
        T t = (T) HelixProperty.convertToTypedInstance(key.getTypeClass(), record);
        childValues.add(t);
      }
    }
  }
  return childValues;
}
 
Example 14
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends HelixProperty> boolean[] setChildren(List<PropertyKey> keys, List<T> children) {
  int options = -1;
  List<String> paths = new ArrayList<String>();
  List<ZNRecord> records = new ArrayList<ZNRecord>();

  List<List<String>> bucketizedPaths =
      new ArrayList<List<String>>(Collections.<List<String>> nCopies(keys.size(), null));
  List<List<ZNRecord>> bucketizedRecords =
      new ArrayList<List<ZNRecord>>(Collections.<List<ZNRecord>> nCopies(keys.size(), null));

  for (int i = 0; i < keys.size(); i++) {
    PropertyKey key = keys.get(i);
    PropertyType type = key.getType();
    String path = key.getPath();
    paths.add(path);
    options = constructOptions(type);

    HelixProperty value = children.get(i);

    switch (type) {
    case EXTERNALVIEW:
      if (value.getBucketSize() == 0) {
        records.add(value.getRecord());
      } else {

        ZNRecord metaRecord = new ZNRecord(value.getId());
        metaRecord.setSimpleFields(value.getRecord().getSimpleFields());
        records.add(metaRecord);

        ZNRecordBucketizer bucketizer = new ZNRecordBucketizer(value.getBucketSize());

        Map<String, ZNRecord> map = bucketizer.bucketize(value.getRecord());
        List<String> childBucketizedPaths = new ArrayList<String>();
        List<ZNRecord> childBucketizedRecords = new ArrayList<ZNRecord>();
        for (String bucketName : map.keySet()) {
          childBucketizedPaths.add(path + "/" + bucketName);
          childBucketizedRecords.add(map.get(bucketName));
        }
        bucketizedPaths.set(i, childBucketizedPaths);
        bucketizedRecords.set(i, childBucketizedRecords);
      }
      break;
    case STATEMODELDEFS:
      if (value.isValid()) {
        records.add(value.getRecord());
      }
      break;
    default:
      records.add(value.getRecord());
      break;
    }
  }

  // set non-bucketized nodes or parent nodes of bucketized nodes
  boolean success[] = _baseDataAccessor.setChildren(paths, records, options);

  // set bucketized nodes
  List<String> allBucketizedPaths = new ArrayList<String>();
  List<ZNRecord> allBucketizedRecords = new ArrayList<ZNRecord>();

  for (int i = 0; i < keys.size(); i++) {
    if (success[i] && bucketizedPaths.get(i) != null) {
      allBucketizedPaths.addAll(bucketizedPaths.get(i));
      allBucketizedRecords.addAll(bucketizedRecords.get(i));
    }
  }

  // TODO: set success accordingly
  _baseDataAccessor.setChildren(allBucketizedPaths, allBucketizedRecords, options);

  return success;
}
 
Example 15
Source File: TestInstanceValidationUtil.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(Object o) {
  PropertyKey propertyKey = (PropertyKey) o;

  return this.propertyType == propertyKey.getType();
}