Skip to content

Commit

Permalink
[FAB-7054] more flexibility setting Kafka.Version
Browse files Browse the repository at this point in the history
Synced Kafka version support to match Fabric v1.1.

- 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.

- Updated the sarama Kafka client library version to
  match the version in master.

Change-Id: I0e10809cc572ba560995c47718c3fbc8f9866494
Signed-off-by: Luis Sanchez <sanchezl@us.ibm.com>
  • Loading branch information
Luis Sanchez committed Nov 27, 2017
1 parent dc8d323 commit 9bf243e
Show file tree
Hide file tree
Showing 19 changed files with 1,090 additions and 46 deletions.
17 changes: 15 additions & 2 deletions common/viperutil/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,34 @@ func TestKafkaVersionDecode(t *testing.T) {
expected sarama.KafkaVersion
errExpected bool
}{
{"0.8", sarama.KafkaVersion{}, true},
{"0.8.2.0", sarama.V0_8_2_0, false},
{"0.8.2.1", sarama.V0_8_2_1, false},
{"0.8.2.2", sarama.V0_8_2_2, false},
{"0.9.0.0", sarama.V0_9_0_0, false},
{"0.9", sarama.V0_9_0_0, false},
{"0.9.0", sarama.V0_9_0_0, false},
{"0.9.0.1", sarama.V0_9_0_1, false},
{"0.9.0.3", sarama.V0_9_0_1, false},
{"0.10.0.0", sarama.V0_10_0_0, false},
{"0.10", sarama.V0_10_0_0, false},
{"0.10.0", sarama.V0_10_0_0, false},
{"0.10.0.1", sarama.V0_10_0_1, false},
{"0.10.1.0", sarama.V0_10_1_0, false},
{"Unsupported", sarama.KafkaVersion{}, true},
{"0.10.2.0", sarama.V0_10_2_0, false},
{"0.10.2.1", sarama.V0_10_2_0, false},
{"0.10.2.2", sarama.V0_10_2_0, false},
{"0.10.2.3", sarama.V0_10_2_0, false},
{"0.11", sarama.KafkaVersion{}, true},
{"0.11.0", sarama.KafkaVersion{}, true},
{"0.11.0.0", sarama.KafkaVersion{}, true},
{"Malformed", sarama.KafkaVersion{}, true},
}

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

data := fmt.Sprintf("---\nInner:\n Version: %s", tc.data)
data := fmt.Sprintf("---\nInner:\n Version: '%s'", tc.data)
err := config.ReadConfig(bytes.NewReader([]byte(data)))
if err != nil {
t.Fatalf("Error reading config: %s", err)
Expand Down
47 changes: 28 additions & 19 deletions common/viperutil/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/pem"

"github.com/Shopify/sarama"
version "github.com/hashicorp/go-version"
"github.com/hyperledger/fabric/common/flogging"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
Expand Down Expand Up @@ -245,31 +246,39 @@ func pemBlocksFromFileDecodeHook() mapstructure.DecodeHookFunc {
}
}

var kafkaVersionConstraints map[sarama.KafkaVersion]version.Constraints

func init() {
kafkaVersionConstraints = make(map[sarama.KafkaVersion]version.Constraints)
kafkaVersionConstraints[sarama.V0_8_2_0], _ = version.NewConstraint(">=0.8.2,<0.8.2.1")
kafkaVersionConstraints[sarama.V0_8_2_1], _ = version.NewConstraint(">=0.8.2.1,<0.8.2.2")
kafkaVersionConstraints[sarama.V0_8_2_2], _ = version.NewConstraint(">=0.8.2.2,<0.9.0.0")
kafkaVersionConstraints[sarama.V0_9_0_0], _ = version.NewConstraint(">=0.9.0.0,<0.9.0.1")
kafkaVersionConstraints[sarama.V0_9_0_1], _ = version.NewConstraint(">=0.9.0.1,<0.10.0.0")
kafkaVersionConstraints[sarama.V0_10_0_0], _ = version.NewConstraint(">=0.10.0.0,<0.10.0.1")
kafkaVersionConstraints[sarama.V0_10_0_1], _ = version.NewConstraint(">=0.10.0.1,<0.10.1.0")
kafkaVersionConstraints[sarama.V0_10_1_0], _ = version.NewConstraint(">=0.10.1.0,<0.10.2.0")
kafkaVersionConstraints[sarama.V0_10_2_0], _ = version.NewConstraint(">=0.10.2.0,<0.11.0.0")
}

func kafkaVersionDecodeHook() mapstructure.DecodeHookFunc {
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String || t != reflect.TypeOf(sarama.KafkaVersion{}) {
return data, nil
}
switch data {
case "0.8.2.0":
return sarama.V0_8_2_0, nil
case "0.8.2.1":
return sarama.V0_8_2_1, nil
case "0.8.2.2":
return sarama.V0_8_2_2, nil
case "0.9.0.0":
return sarama.V0_9_0_0, nil
case "0.9.0.1":
return sarama.V0_9_0_1, nil
case "0.10.0.0":
return sarama.V0_10_0_0, nil
case "0.10.0.1":
return sarama.V0_10_0_1, nil
case "0.10.1.0":
return sarama.V0_10_1_0, nil
default:
return nil, fmt.Errorf("Unsupported Kafka version: '%s'", data)

v, err := version.NewVersion(data.(string))
if err != nil {
return nil, fmt.Errorf("Unable to parse Kafka version: %s", err)
}

for kafkaVersion, constraints := range kafkaVersionConstraints {
if constraints.Check(v) {
return kafkaVersion, nil
}
}

return nil, fmt.Errorf("Unsupported Kafka version: '%s'", data)
}
}

Expand Down
13 changes: 6 additions & 7 deletions docs/source/kafka.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@ Additional considerations
Supported Kafka versions and upgrading
--------------------------------------

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

* ``Version: 0.9.0.1``
* ``Version: 0.10.0.0``
* ``Version: 0.10.0.1``
* ``Version: 0.10.1.0``
* ``Version: 0.10.2.0``
* ``Version: 0.9.0``
* ``Version: 0.10.0``
* ``Version: 0.10.1``
* ``Version: 0.10.2``

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``.
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``.

Debugging
---------
Expand Down
16 changes: 15 additions & 1 deletion vendor/github.com/Shopify/sarama/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/Shopify/sarama/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/Shopify/sarama/async_producer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/github.com/Shopify/sarama/broker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions vendor/github.com/Shopify/sarama/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions vendor/github.com/Shopify/sarama/consumer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions vendor/github.com/Shopify/sarama/crc32_field.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion vendor/github.com/Shopify/sarama/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/Shopify/sarama/mocks/consumer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/Shopify/sarama/utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9bf243e

Please sign in to comment.