ruby-kafka-ec2 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8620cd2b3622df81ce8105a45930522ff9c9b433efbcba2c356c52bd7bdc7df
4
- data.tar.gz: 5ccbcce906e3f000d445fe3a2135f4da2d7f7dbbbe3c7267849fc59bd9aabc9d
3
+ metadata.gz: d731fe0da3282726a8bb898f98f0ab3fe0133463662d9e7bc3c536c965bfc278
4
+ data.tar.gz: e260eb39399056cc48cdf8b7b985b037cdb60eafc8f495771d7949e7eb581a52
5
5
  SHA512:
6
- metadata.gz: d085d3c8a0bcd5a3e0ab00f1b89d3cba53af8f5a33e8bf78bbfa5e03e96c027a3a747ef34a730400e4567e3b6533615fc6477c57eb4164da2a3d9f1b43ab71cb
7
- data.tar.gz: a1343b55e46e61346f1b5b179b15cfcc838ecfb39650063369fa9dd92c0b786b3872bcf0330bc7039ca66a856e9f3657e3249a6dc927460bb2f158186fb4930c
6
+ metadata.gz: 3f31a1d280d2f49b864fbc4068a4f198952180101973d2a93c708ff621238f83476dac72e8c302427f3413a6a9761f81b5a703b8020aee3edba5bf2a42e0a967
7
+ data.tar.gz: 9d4cda171d25754e42bda7a90c4d7b9c8a77fa8fc984a5546ccb1cbc10b4ccf10999f75c6dbaa7ecd4fdecdde285a3ccec0ead4071326564f6527382853edbe0
data/README.md CHANGED
@@ -77,6 +77,48 @@ consumer = Kafka::EC2.with_assignment_strategy_factory(assignment_strategy_facto
77
77
  end
78
78
  ```
79
79
 
80
+ You can also specify weights for each combination of availability zones and instance families:
81
+
82
+ ```ruby
83
+ assignment_strategy_factory = Kafka::EC2::MixedInstanceAssignmentStrategyFactory.new(
84
+ weights: ->() {
85
+ db_cluster = rds.describe_db_clusters(filters: [
86
+ { name: "db-cluster-id", values: [ENV["RDS_CLUSTER"]] },
87
+ ]).db_clusters.first
88
+ db_instance_id = db_cluster.db_cluster_members.find { |m| m.is_cluster_writer }.db_instance_identifier
89
+ db_instance = rds.describe_db_instances(filters: [
90
+ { name: "db-cluster-id", values: [ENV["RDS_CLUSTER"]] },
91
+ { name: "db-instance-id", values: [db_instance_id] },
92
+ ]).db_instances.first
93
+
94
+ weights_for_writer_az = {
95
+ "r4" => 1.00,
96
+ "r5" => 1.20,
97
+ "m5" => 1.35,
98
+ "c5" => 1.50,
99
+ }
100
+ weights_for_other_az = {
101
+ "r4" => 0.40,
102
+ "r5" => 0.70,
103
+ "m5" => 0.80,
104
+ "c5" => 1.00,
105
+ }
106
+ if db_instance.availability_zone == "ap-northeast-1a"
107
+ {
108
+ "ap-northeast-1a" => weights_for_writer_az,
109
+ "ap-northeast-1c" => weights_for_other_az,
110
+ }
111
+ else
112
+ {
113
+ "ap-northeast-1a" => weights_for_other_az,
114
+ "ap-northeast-1c" => weights_for_writer_az,,
115
+ }
116
+ end
117
+ },
118
+ )
119
+ ```
120
+
121
+
80
122
  ## Development
81
123
 
82
124
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -18,10 +18,16 @@ module Kafka
18
18
  # is the availability zone and whose value is the weight. If the object is a proc,
19
19
  # it must returns such a hash and the proc is called every time the method "assign"
20
20
  # is called.
21
- def initialize(cluster:, instance_family_weights:, availability_zone_weights:)
21
+ # @param weights [Hash{String => Hash{String => Numeric}}, Proc] a hash whose the key
22
+ # is the availability zone or the instance family and whose value is the hash like
23
+ # instance_family_weights or availability_zone_weights. If the object is a proc,
24
+ # it must returns such a hash and the proc is called every time the method "assign"
25
+ # is called.
26
+ def initialize(cluster:, instance_family_weights: {}, availability_zone_weights: {}, weights: {})
22
27
  @cluster = cluster
23
28
  @instance_family_weights = instance_family_weights
24
29
  @availability_zone_weights = availability_zone_weights
30
+ @weights = weights
25
31
  end
26
32
 
27
33
  # Assign the topic partitions to the group members.
@@ -38,12 +44,13 @@ module Kafka
38
44
 
39
45
  instance_family_to_capacity = @instance_family_weights.is_a?(Proc) ? @instance_family_weights.call() : @instance_family_weights
40
46
  az_to_capacity = @availability_zone_weights.is_a?(Proc) ? @availability_zone_weights.call() : @availability_zone_weights
47
+ weights = @weights.is_a?(Proc) ? @weights.call() : @weights
41
48
  members.each do |member_id|
42
49
  group_assignment[member_id] = Protocol::MemberAssignment.new
43
50
 
44
51
  instance_id, instance_type, az = member_id_to_metadata[member_id].split(",")
45
52
  instance_id_to_member_ids[instance_id] << member_id
46
- capacity = calculate_capacity(instance_type, az, instance_family_to_capacity, az_to_capacity)
53
+ capacity = calculate_capacity(instance_type, az, instance_family_to_capacity, az_to_capacity, weights)
47
54
  instance_id_to_capacity[instance_id] += capacity
48
55
  total_capacity += capacity
49
56
  end
@@ -86,9 +93,11 @@ module Kafka
86
93
 
87
94
  private
88
95
 
89
- def calculate_capacity(instance_type, az, instance_family_to_capacity, az_to_capacity)
96
+ def calculate_capacity(instance_type, az, instance_family_to_capacity, az_to_capacity, weights)
90
97
  instance_family, _ = instance_type.split(".")
91
- instance_family_to_capacity.fetch(instance_family, 1) * az_to_capacity.fetch(az, 1)
98
+
99
+ capacity = weights.dig(az, instance_family) || weights.dig(instance_family, az)
100
+ capacity || instance_family_to_capacity.fetch(instance_family, 1) * az_to_capacity.fetch(az, 1)
92
101
  end
93
102
  end
94
103
  end
@@ -7,7 +7,9 @@ module Kafka
7
7
  class MixedInstanceAssignmentStrategyFactory
8
8
  # @param instance_family_weights [Hash, Proc]
9
9
  # @param availability_zone_weights [Hash, Proc]
10
- def initialize(instance_family_weights: {}, availability_zone_weights: {})
10
+ # @param weights [Hash, Proc]
11
+ # @see Kafka::EC2::MixedInstanceAssignmentStrategy#initialize
12
+ def initialize(instance_family_weights: {}, availability_zone_weights: {}, weights: {})
11
13
  @instance_family_weights = instance_family_weights
12
14
  @availability_zone_weights = availability_zone_weights
13
15
  end
@@ -1,5 +1,5 @@
1
1
  module Kafka
2
2
  class EC2
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-kafka-ec2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - abicky
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-23 00:00:00.000000000 Z
11
+ date: 2020-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-kafka