Skip to content

Commit 4f6e4e6

Browse files
author
Luis Sanchez
committed
[FAB-4619] enable specifying orderer kafka version
Kafka version can now be set in orderer.yaml, eg: Kakfa: Version: 0.10.0.0 Default value is '0.9.0.1'. Change-Id: Ieb69b003b400bff9c7918743e99ce76c4ad4363c Signed-off-by: Luis Sanchez <[email protected]>
1 parent f49218d commit 4f6e4e6

File tree

6 files changed

+109
-21
lines changed

6 files changed

+109
-21
lines changed

common/viperutil/config_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626
"testing"
2727

28+
"github.com/Shopify/sarama"
2829
"github.com/hyperledger/fabric/orderer/mocks/util"
2930
"github.com/spf13/viper"
3031
)
@@ -70,6 +71,63 @@ func TestEnvSlice(t *testing.T) {
7071
}
7172
}
7273

74+
func TestKafkaVersionDecode(t *testing.T) {
75+
76+
type testKafkaVersion struct {
77+
Inner struct {
78+
Version sarama.KafkaVersion
79+
}
80+
}
81+
82+
config := viper.New()
83+
config.SetConfigType("yaml")
84+
85+
testCases := []struct {
86+
data string
87+
expected sarama.KafkaVersion
88+
errExpected bool
89+
}{
90+
{"0.8.2.0", sarama.V0_8_2_0, false},
91+
{"0.8.2.1", sarama.V0_8_2_1, false},
92+
{"0.8.2.2", sarama.V0_8_2_2, false},
93+
{"0.9.0.0", sarama.V0_9_0_0, false},
94+
{"0.9.0.1", sarama.V0_9_0_1, false},
95+
{"0.10.0.0", sarama.V0_10_0_0, false},
96+
{"0.10.0.1", sarama.V0_10_0_1, false},
97+
{"0.10.1.0", sarama.V0_10_1_0, false},
98+
{"Unsupported", sarama.KafkaVersion{}, true},
99+
}
100+
101+
for _, tc := range testCases {
102+
t.Run(tc.data, func(t *testing.T) {
103+
104+
data := fmt.Sprintf("---\nInner:\n Version: %s", tc.data)
105+
err := config.ReadConfig(bytes.NewReader([]byte(data)))
106+
if err != nil {
107+
t.Fatalf("Error reading config: %s", err)
108+
}
109+
110+
var uconf testKafkaVersion
111+
err = EnhancedExactUnmarshal(config, &uconf)
112+
113+
if tc.errExpected {
114+
if err == nil {
115+
t.Fatalf("Should have failed to unmarshal")
116+
}
117+
} else {
118+
if err != nil {
119+
t.Fatalf("Failed to unmarshal with: %s", err)
120+
}
121+
if uconf.Inner.Version != tc.expected {
122+
t.Fatalf("Did not get back the right kafka version, expected: %v got %v", tc.expected, uconf.Inner.Version)
123+
}
124+
}
125+
126+
})
127+
}
128+
129+
}
130+
73131
type testByteSize struct {
74132
Inner struct {
75133
ByteSize uint32

common/viperutil/config_util.go

+30
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"encoding/json"
3030
"encoding/pem"
3131

32+
"github.com/Shopify/sarama"
3233
"github.com/hyperledger/fabric/common/flogging"
3334
"github.com/mitchellh/mapstructure"
3435
"github.com/spf13/viper"
@@ -254,6 +255,34 @@ func pemBlocksFromFileDecodeHook() mapstructure.DecodeHookFunc {
254255
}
255256
}
256257

258+
func kafkaVersionDecodeHook() mapstructure.DecodeHookFunc {
259+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
260+
if f.Kind() != reflect.String || t != reflect.TypeOf(sarama.KafkaVersion{}) {
261+
return data, nil
262+
}
263+
switch data {
264+
case "0.8.2.0":
265+
return sarama.V0_8_2_0, nil
266+
case "0.8.2.1":
267+
return sarama.V0_8_2_1, nil
268+
case "0.8.2.2":
269+
return sarama.V0_8_2_2, nil
270+
case "0.9.0.0":
271+
return sarama.V0_9_0_0, nil
272+
case "0.9.0.1":
273+
return sarama.V0_9_0_1, nil
274+
case "0.10.0.0":
275+
return sarama.V0_10_0_0, nil
276+
case "0.10.0.1":
277+
return sarama.V0_10_0_1, nil
278+
case "0.10.1.0":
279+
return sarama.V0_10_1_0, nil
280+
default:
281+
return nil, fmt.Errorf("Unsupported Kafka version: '%s'", data)
282+
}
283+
}
284+
}
285+
257286
// EnhancedExactUnmarshal is intended to unmarshal a config file into a structure
258287
// producing error when extraneous variables are introduced and supporting
259288
// the time.Duration type
@@ -274,6 +303,7 @@ func EnhancedExactUnmarshal(v *viper.Viper, output interface{}) error {
274303
byteSizeDecodeHook(),
275304
stringFromFileDecodeHook(),
276305
pemBlocksFromFileDecodeHook(),
306+
kafkaVersionDecodeHook(),
277307
),
278308
}
279309

docs/source/kafka.rst

+9-16
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,15 @@ Supported Kafka versions for v1 are ``0.9`` and ``0.10``. (Fabric uses the
185185
version of it that supports Kafka 0.9 and 0.10.)
186186

187187
Out of the box the Kafka version defaults to ``0.9.0.1``. If you wish to use a
188-
different supported version, you will have to edit the source code (modify the
189-
``Version`` field of the ``defaults`` struct in
190-
``orderer/localconfig/config.go``) and rebuild the ``orderer`` binary. For
191-
example, if you wish to run the ordering service in a Kafka cluster running
192-
0.10.0.1, you would edit the file like so:
193-
194-
::
195-
196-
...
197-
Verbose: false,
198-
Version: sarama.V0_10_0_1,
199-
TLS: TLS{
200-
...
201-
202-
And then rebuild the binary. (This process will be improved with
203-
`FAB-4619 <https://jira.hyperledger.org/browse/FAB-4619>`_.)
188+
different supported version, specify a supported version using the
189+
``Kafka.Version`` key in ``orderer.yaml``.
190+
191+
The current supported Kafka versions are:
192+
193+
* ``Version: 0.9.0.1``
194+
* ``Version: 0.10.0.0``
195+
* ``Version: 0.10.0.1``
196+
* ``Version: 0.10.1.0``
204197

205198
Debugging
206199
---------

examples/cluster/config/orderer.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ General:
6767

6868
# LocalMSPID is the identity to register the local MSP material with the MSP
6969
# manager. IMPORTANT: The local MSP ID of an orderer needs to match the MSP
70-
# ID of one of the organizations defined in the orderer system channel's
70+
# ID of one of the organizations defined in the orderer system channel's
7171
# /Channel/Orderer configuration. The sample organization defined in the
7272
# sample configuration provided has an MSP ID of "DEFAULT".
7373
LocalMSPID: OrdererMSP
@@ -221,3 +221,6 @@ Kafka:
221221
# following "File" key and specify the file name from which to load the
222222
# value of RootCAs.
223223
#File: path/to/RootCAs
224+
225+
# Kafka version of the Kafka cluster brokers (defaults to 0.9.0.1)
226+
Version:

orderer/localconfig/config.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,11 @@ func (c *TopLevel) completeInitialization(configDir string) {
340340
logger.Infof("Kafka.Retry.Consumer.RetryBackoff unset, setting to %v", defaults.Kafka.Retry.Consumer.RetryBackoff)
341341
c.Kafka.Retry.Consumer.RetryBackoff = defaults.Kafka.Retry.Consumer.RetryBackoff
342342

343-
default:
344-
// A bit hacky, but its type makes it impossible to test for a nil value.
345-
// This may be overwritten by the Kafka orderer upon instantiation.
343+
case c.Kafka.Version == sarama.KafkaVersion{}:
344+
logger.Infof("Kafka.Version unset, setting to %v", defaults.Kafka.Version)
346345
c.Kafka.Version = defaults.Kafka.Version
346+
347+
default:
347348
return
348349
}
349350
}

sampleconfig/orderer.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ General:
6767

6868
# LocalMSPID is the identity to register the local MSP material with the MSP
6969
# manager. IMPORTANT: The local MSP ID of an orderer needs to match the MSP
70-
# ID of one of the organizations defined in the orderer system channel's
70+
# ID of one of the organizations defined in the orderer system channel's
7171
# /Channel/Orderer configuration. The sample organization defined in the
7272
# sample configuration provided has an MSP ID of "DEFAULT".
7373
LocalMSPID: DEFAULT
@@ -221,3 +221,6 @@ Kafka:
221221
# following "File" key and specify the file name from which to load the
222222
# value of RootCAs.
223223
#File: path/to/RootCAs
224+
225+
# Kafka version of the Kafka cluster brokers (defaults to 0.9.0.1)
226+
Version:

0 commit comments

Comments
 (0)