Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAMZA-2688: [Elasticity] introduce configs and sub-partition concept aka SystemStreamPartitionKeyHash #1531

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class IncomingMessageEnvelope {
private long eventTime = 0L;
// the timestamp when this event is pickedup by samza, 0 means unassgined
private long arrivalTime = 0L;
// SystemStreamPartitionKeyHash for Elasticity needs KeyHash which is computed from the key or offset if key is null.
// since both key and offset are final, pre-computing the hashcode needed for computing KeyHash.
private final int hashCodeForKeyHashComputation;

/**
* Constructs a new IncomingMessageEnvelope from specified components.
Expand Down Expand Up @@ -71,6 +74,7 @@ public IncomingMessageEnvelope(SystemStreamPartition systemStreamPartition, Stri
this.message = message;
this.size = size;
this.arrivalTime = Instant.now().toEpochMilli();
this.hashCodeForKeyHashComputation = key != null ? key.hashCode() : offset != null ? offset.hashCode() : hashCode();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a benefit to caching and storing it, rather than computing and exposing it via a function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did consider this originally but if there are multiple calls to getSystemStreamPartitionKeyHash then it would be worth having this cached.

}

/**
Expand Down Expand Up @@ -111,6 +115,15 @@ public SystemStreamPartition getSystemStreamPartition() {
return systemStreamPartition;
}

// Returns the portion of SSP represented by SystemStreamPartitionKeyHash that this envelope should belong to
// when each SSP is divved into "numberOfKeyHashes" portions.
public SystemStreamPartitionKeyHash getSystemStreamPartitionKeyHash(int numberOfKeyHashes) {
int keyHash = Math.abs(hashCodeForKeyHashComputation % numberOfKeyHashes);
SystemStreamPartitionKeyHash systemStreamPartitionKeyHash =
new SystemStreamPartitionKeyHash(systemStreamPartition, keyHash);
return systemStreamPartitionKeyHash;
}

/**
* Offset associated with this message, provided by the system consumer that consumed the message.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.samza.system;

import org.apache.samza.Partition;

/**
* Aggregate object representing a portion of {@link SystemStreamPartition} consisting of envelopes within the
* SystemStreamPartition that have envelope.key % job's elasticity factor = keyHash of this object.
*/
public class SystemStreamPartitionKeyHash extends SystemStreamPartition {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add the changes (i.e., keyhash) within SystemStreamPartition class itself?
Since logically this is representing a key-range (which is pretty such a "partition" of data, albeit different from the input kafka-partition)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

theoretically this should be possible. But the problem I forsee is with the serde of job model where the backwards compatability of reading an old job model containing old SSP serde might get impacted with the new defn and serde of SSP. Let me test this and get back on this thread.

protected final int keyHash;

/**
* Constructs a Samza stream partition KeyHash object from specified components.
* @param system The name of the system of which this stream is associated with.
* @param stream The name of the stream as specified in the stream configuration file.
* @param partition The partition in the stream of which this object is associated with.
* @param keyHash The KeyHash of the partition (aka portion of SSP) this object is associated with.
*/
public SystemStreamPartitionKeyHash(String system, String stream, Partition partition, int keyHash) {
super(system, stream, partition);
this.keyHash = keyHash;
}
/**
* Constructs a Samza stream partition object based upon an existing Samza stream partition and a keyHash.
* @param systemStreamPartition Reference to an already existing Samza stream partition.
* @param keyHash the KeyHash of the systemStreamPartition for this object.
*/
public SystemStreamPartitionKeyHash(SystemStreamPartition systemStreamPartition, int keyHash) {
super(systemStreamPartition);
this.keyHash = keyHash;
}

/**
* Constructs a Samza stream partition KeyHash object based upon an existing Samza stream partition keyHash.
* @param other Reference to an already existing Samza stream partition KeyHash.
*/
public SystemStreamPartitionKeyHash(SystemStreamPartitionKeyHash other) {
this(other.getSystem(), other.getStream(), other.getPartition(), other.keyHash);
}

/**
* Constructs a Samza stream partition object based upon another Samza stream and a specified partition.
* @param other Reference to an already existing Samza stream.
* @param partition Reference to an already existing Samza partition.
* @param keyHash the KeyHash of the systemStreamPartition for this object.
*/
public SystemStreamPartitionKeyHash(SystemStream other, Partition partition, int keyHash) {
super(other, partition);
this.keyHash = keyHash;
}

public int getKeyHash() {
return this.keyHash;
}

@Override
public int hashCode() {
return hash;
}

private int computeHashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + keyHash;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
SystemStreamPartitionKeyHash other = (SystemStreamPartitionKeyHash) obj;
if (keyHash != other.keyHash) {
return false;
}
return true;
}

@Override
public String toString() {
return "SystemStreamPartitionKeyHash [" + system + ", " + stream + ", " + partition.getPartitionId() + ", " + keyHash + "]";
}

@Override
public int compareTo(SystemStreamPartition that) {
SystemStreamPartitionKeyHash other = (SystemStreamPartitionKeyHash) that;
if (this.system.compareTo(other.system) < 0) {
return -1;
} else if (this.system.compareTo(other.system) > 0) {
return 1;
}

if (this.stream.compareTo(other.stream) < 0) {
return -1;
} else if (this.stream.compareTo(other.stream) > 0) {
return 1;
}

if (this.partition.compareTo(other.partition) < 0) {
return -1;
} else if (this.partition.compareTo(other.partition) > 0) {
return 1;
}

if (this.keyHash < other.keyHash) {
return -1;
} else if (this.keyHash > other.keyHash) {
return 1;
}

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public class TaskConfig extends MapConfig {
"task.transactional.state.retain.existing.state";
private static final boolean DEFAULT_TRANSACTIONAL_STATE_RETAIN_EXISTING_STATE = true;

// Job Elasticity related configs
// Take effect only when job.elasticity.factor is > 1. otherwise there is no elasticity
private static final String TASK_ELASTICITY_FACTOR = "task.elasticity.factor";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this more like a task-to-partition mapping factor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am looking at this more as a split multiple. As in if the factor = 2 then split each original task into 2 virtual/elastic tasks. It can also be looked at as task-to-partition/ssp factor where factor = 2 means each ssp is read by 2 virtual/elastic tasks. would lend the same semantics.

private static final int TASK_ELASTICITY_FACTOR_DEFAULT = 1;

public TaskConfig(Config config) {
super(config);
}
Expand Down Expand Up @@ -379,4 +384,8 @@ public boolean getTransactionalStateRestoreEnabled() {
public boolean getTransactionalStateRetainExistingState() {
return getBoolean(TRANSACTIONAL_STATE_RETAIN_EXISTING_STATE, DEFAULT_TRANSACTIONAL_STATE_RETAIN_EXISTING_STATE);
}

public int getElasticityFactor() {
return getInt(TASK_ELASTICITY_FACTOR, TASK_ELASTICITY_FACTOR_DEFAULT);
}
}