Skip to content

Commit 64974f0

Browse files
author
Luis Sanchez
committed
[FAB-7054] more flexibility setting Kafka.Version
Made the parsing of Kafka.Version in orderer.yaml more forgiving, and mapping the value to a supported underlying value. Vendored hashicorp/go-version utility for parsing version strings. Change-Id: I0e10809cc572ba560995c47718c3fbc8f9866494 Signed-off-by: Luis Sanchez <sanchezl@us.ibm.com>
1 parent cedfdf5 commit 64974f0

File tree

9 files changed

+990
-31
lines changed

9 files changed

+990
-31
lines changed

common/viperutil/config_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,34 @@ func TestKafkaVersionDecode(t *testing.T) {
7777
expected sarama.KafkaVersion
7878
errExpected bool
7979
}{
80+
{"0.8", sarama.KafkaVersion{}, true},
8081
{"0.8.2.0", sarama.V0_8_2_0, false},
8182
{"0.8.2.1", sarama.V0_8_2_1, false},
8283
{"0.8.2.2", sarama.V0_8_2_2, false},
8384
{"0.9.0.0", sarama.V0_9_0_0, false},
85+
{"0.9", sarama.V0_9_0_0, false},
86+
{"0.9.0", sarama.V0_9_0_0, false},
8487
{"0.9.0.1", sarama.V0_9_0_1, false},
88+
{"0.9.0.3", sarama.V0_9_0_1, false},
8589
{"0.10.0.0", sarama.V0_10_0_0, false},
90+
{"0.10", sarama.V0_10_0_0, false},
91+
{"0.10.0", sarama.V0_10_0_0, false},
8692
{"0.10.0.1", sarama.V0_10_0_1, false},
8793
{"0.10.1.0", sarama.V0_10_1_0, false},
8894
{"0.10.2.0", sarama.V0_10_2_0, false},
89-
{"Unsupported", sarama.KafkaVersion{}, true},
95+
{"0.10.2.1", sarama.V0_10_2_0, false},
96+
{"0.10.2.2", sarama.V0_10_2_0, false},
97+
{"0.10.2.3", sarama.V0_10_2_0, false},
98+
{"0.11", sarama.KafkaVersion{}, true},
99+
{"0.11.0", sarama.KafkaVersion{}, true},
100+
{"0.11.0.0", sarama.KafkaVersion{}, true},
101+
{"Malformed", sarama.KafkaVersion{}, true},
90102
}
91103

92104
for _, tc := range testCases {
93105
t.Run(tc.data, func(t *testing.T) {
94106

95-
data := fmt.Sprintf("---\nInner:\n Version: %s", tc.data)
107+
data := fmt.Sprintf("---\nInner:\n Version: '%s'", tc.data)
96108
err := config.ReadConfig(bytes.NewReader([]byte(data)))
97109
if err != nil {
98110
t.Fatalf("Error reading config: %s", err)

common/viperutil/config_util.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/pem"
2121

2222
"github.com/Shopify/sarama"
23+
version "github.com/hashicorp/go-version"
2324
"github.com/hyperledger/fabric/common/flogging"
2425
"github.com/mitchellh/mapstructure"
2526
"github.com/spf13/viper"
@@ -245,33 +246,39 @@ func pemBlocksFromFileDecodeHook() mapstructure.DecodeHookFunc {
245246
}
246247
}
247248

249+
var kafkaVersionConstraints map[sarama.KafkaVersion]version.Constraints
250+
251+
func init() {
252+
kafkaVersionConstraints = make(map[sarama.KafkaVersion]version.Constraints)
253+
kafkaVersionConstraints[sarama.V0_8_2_0], _ = version.NewConstraint(">=0.8.2,<0.8.2.1")
254+
kafkaVersionConstraints[sarama.V0_8_2_1], _ = version.NewConstraint(">=0.8.2.1,<0.8.2.2")
255+
kafkaVersionConstraints[sarama.V0_8_2_2], _ = version.NewConstraint(">=0.8.2.2,<0.9.0.0")
256+
kafkaVersionConstraints[sarama.V0_9_0_0], _ = version.NewConstraint(">=0.9.0.0,<0.9.0.1")
257+
kafkaVersionConstraints[sarama.V0_9_0_1], _ = version.NewConstraint(">=0.9.0.1,<0.10.0.0")
258+
kafkaVersionConstraints[sarama.V0_10_0_0], _ = version.NewConstraint(">=0.10.0.0,<0.10.0.1")
259+
kafkaVersionConstraints[sarama.V0_10_0_1], _ = version.NewConstraint(">=0.10.0.1,<0.10.1.0")
260+
kafkaVersionConstraints[sarama.V0_10_1_0], _ = version.NewConstraint(">=0.10.1.0,<0.10.2.0")
261+
kafkaVersionConstraints[sarama.V0_10_2_0], _ = version.NewConstraint(">=0.10.2.0,<0.11.0.0")
262+
}
263+
248264
func kafkaVersionDecodeHook() mapstructure.DecodeHookFunc {
249265
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
250266
if f.Kind() != reflect.String || t != reflect.TypeOf(sarama.KafkaVersion{}) {
251267
return data, nil
252268
}
253-
switch data {
254-
case "0.8.2.0":
255-
return sarama.V0_8_2_0, nil
256-
case "0.8.2.1":
257-
return sarama.V0_8_2_1, nil
258-
case "0.8.2.2":
259-
return sarama.V0_8_2_2, nil
260-
case "0.9.0.0":
261-
return sarama.V0_9_0_0, nil
262-
case "0.9.0.1":
263-
return sarama.V0_9_0_1, nil
264-
case "0.10.0.0":
265-
return sarama.V0_10_0_0, nil
266-
case "0.10.0.1":
267-
return sarama.V0_10_0_1, nil
268-
case "0.10.1.0":
269-
return sarama.V0_10_1_0, nil
270-
case "0.10.2.0":
271-
return sarama.V0_10_2_0, nil
272-
default:
273-
return nil, fmt.Errorf("Unsupported Kafka version: '%s'", data)
269+
270+
v, err := version.NewVersion(data.(string))
271+
if err != nil {
272+
return nil, fmt.Errorf("Unable to parse Kafka version: %s", err)
273+
}
274+
275+
for kafkaVersion, constraints := range kafkaVersionConstraints {
276+
if constraints.Check(v) {
277+
return kafkaVersion, nil
278+
}
274279
}
280+
281+
return nil, fmt.Errorf("Unsupported Kafka version: '%s'", data)
275282
}
276283
}
277284

docs/source/kafka.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,14 @@ Additional considerations
7272
Supported Kafka versions and upgrading
7373
--------------------------------------
7474

75-
Fabric uses the `sarama client library <https://github.com/Shopify/sarama>`_ and vendors a version of it that supportes the following Kafka client versions:
75+
Fabric uses the `sarama client library <https://github.com/Shopify/sarama>`_ and vendors a version of it that supports the following Kafka client versions:
7676

77-
* ``Version: 0.9.0.1``
78-
* ``Version: 0.10.0.0``
79-
* ``Version: 0.10.0.1``
80-
* ``Version: 0.10.1.0``
81-
* ``Version: 0.10.2.0``
77+
* ``Version: 0.9.0``
78+
* ``Version: 0.10.0``
79+
* ``Version: 0.10.1``
80+
* ``Version: 0.10.2``
8281

83-
The sample Kafka server image provided by Fabric contains Kafka server version ``0.10.2.0``. Out of the box, Fabric's ordering service nodes default to configuring their embedded Kafka client to match this version. If you are not using the sample Kafka server image provided by Fabric, ensure that you configure a Kafka client version that is compatible with your Kafka server using the ``Kafka.Version`` key in ``orderer.yaml``.
82+
The sample Kafka server image provided by Fabric contains Kafka server version ``0.10.2``. Out of the box, Fabric's ordering service nodes default to configuring their embedded Kafka client to match this version. If you are not using the sample Kafka server image provided by Fabric, ensure that you configure a Kafka client version that is compatible with your Kafka server using the ``Kafka.Version`` key in ``orderer.yaml``.
8483

8584
Debugging
8685
---------

0 commit comments

Comments
 (0)