Skip to content

Commit f803a9f

Browse files
committed
[FAB-2591] Introduce file-ledger option
https://jira.hyperledger.org/browse/FAB-2591 This changeset allows the user to choose the file ledger, introduced in a previous changeset. Change-Id: Id518961af10fddb4954291592cf53b12f1e3d1fa Signed-off-by: Kostas Christidis <[email protected]>
1 parent 3000b25 commit f803a9f

File tree

4 files changed

+218
-47
lines changed

4 files changed

+218
-47
lines changed

orderer/main.go

+1-46
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package main
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"log"
2322
"net"
2423
"net/http"
@@ -31,15 +30,9 @@ import (
3130
"github.com/hyperledger/fabric/core/comm"
3231
"github.com/hyperledger/fabric/orderer/common/bootstrap/file"
3332
"github.com/hyperledger/fabric/orderer/kafka"
34-
"github.com/hyperledger/fabric/orderer/ledger"
35-
jsonledger "github.com/hyperledger/fabric/orderer/ledger/json"
36-
ramledger "github.com/hyperledger/fabric/orderer/ledger/ram"
3733
"github.com/hyperledger/fabric/orderer/localconfig"
3834
"github.com/hyperledger/fabric/orderer/multichain"
3935
"github.com/hyperledger/fabric/orderer/sbft"
40-
"github.com/hyperledger/fabric/orderer/sbft/backend"
41-
sbftcrypto "github.com/hyperledger/fabric/orderer/sbft/crypto"
42-
"github.com/hyperledger/fabric/orderer/sbft/simplebft"
4336
"github.com/hyperledger/fabric/orderer/solo"
4437
cb "github.com/hyperledger/fabric/protos/common"
4538
ab "github.com/hyperledger/fabric/protos/orderer"
@@ -93,26 +86,7 @@ func main() {
9386
logger.Panic("Failed to initialize local MSP:", err)
9487
}
9588

96-
var lf ledger.Factory
97-
switch conf.General.LedgerType {
98-
case "file":
99-
// just use the json ledger type for now
100-
fallthrough
101-
case "json":
102-
location := conf.FileLedger.Location
103-
if location == "" {
104-
var err error
105-
location, err = ioutil.TempDir("", conf.FileLedger.Prefix)
106-
if err != nil {
107-
logger.Panic("Error creating temp dir:", err)
108-
}
109-
}
110-
lf = jsonledger.New(location)
111-
case "ram":
112-
fallthrough
113-
default:
114-
lf = ramledger.New(int(conf.RAMLedger.HistorySize))
115-
}
89+
lf, _ := createLedgerFactory(conf)
11690

11791
// Are we bootstrapping?
11892
if len(lf.ChainIDs()) == 0 {
@@ -166,22 +140,3 @@ func main() {
166140
logger.Info("Beginning to serve requests")
167141
grpcServer.Start()
168142
}
169-
170-
func makeSbftConsensusConfig(conf *config.TopLevel) *sbft.ConsensusConfig {
171-
cfg := simplebft.Config{N: conf.Genesis.SbftShared.N, F: conf.Genesis.SbftShared.F,
172-
BatchDurationNsec: uint64(conf.Genesis.DeprecatedBatchTimeout),
173-
BatchSizeBytes: uint64(conf.Genesis.DeprecatedBatchSize),
174-
RequestTimeoutNsec: conf.Genesis.SbftShared.RequestTimeoutNsec}
175-
peers := make(map[string][]byte)
176-
for addr, cert := range conf.Genesis.SbftShared.Peers {
177-
peers[addr], _ = sbftcrypto.ParseCertPEM(cert)
178-
}
179-
return &sbft.ConsensusConfig{Consensus: &cfg, Peers: peers}
180-
}
181-
182-
func makeSbftStackConfig(conf *config.TopLevel) *backend.StackConfig {
183-
return &backend.StackConfig{ListenAddr: conf.SbftLocal.PeerCommAddr,
184-
CertFile: conf.SbftLocal.CertFile,
185-
KeyFile: conf.SbftLocal.KeyFile,
186-
DataDir: conf.SbftLocal.DataDir}
187-
}

orderer/orderer.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ General:
3333

3434
# Log Level: The level at which to log. This accepts logging specifications
3535
# per: fabric/docs/Setup/logging-control.md
36-
LogLevel: debug
36+
LogLevel: info
3737

3838
# Genesis method: The method by which to retrieve/generate the genesis
3939
# block. Available values are "provisional", "file". Provisional utilizes

orderer/util.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"io/ioutil"
21+
"os"
22+
"path/filepath"
23+
24+
"github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage"
25+
"github.com/hyperledger/fabric/orderer/ledger"
26+
fileledger "github.com/hyperledger/fabric/orderer/ledger/file"
27+
jsonledger "github.com/hyperledger/fabric/orderer/ledger/json"
28+
ramledger "github.com/hyperledger/fabric/orderer/ledger/ram"
29+
config "github.com/hyperledger/fabric/orderer/localconfig"
30+
"github.com/hyperledger/fabric/orderer/sbft"
31+
"github.com/hyperledger/fabric/orderer/sbft/backend"
32+
sbftcrypto "github.com/hyperledger/fabric/orderer/sbft/crypto"
33+
"github.com/hyperledger/fabric/orderer/sbft/simplebft"
34+
)
35+
36+
func createLedgerFactory(conf *config.TopLevel) (ledger.Factory, string) {
37+
var lf ledger.Factory
38+
var ld string
39+
switch conf.General.LedgerType {
40+
case "file":
41+
ld = conf.FileLedger.Location
42+
if ld == "" {
43+
ld = createTempDir(conf.FileLedger.Prefix)
44+
}
45+
logger.Debug("Ledger dir:", ld)
46+
lf = fileledger.New(ld)
47+
// The file-based ledger stores the blocks for each channel
48+
// in a fsblkstorage.ChainsDir sub-directory that we have
49+
// to create separately. Otherwise the call to the ledger
50+
// Factory's ChainIDs below will fail (dir won't exist).
51+
createSubDir(ld, fsblkstorage.ChainsDir)
52+
case "json":
53+
ld = conf.FileLedger.Location
54+
if ld == "" {
55+
ld = createTempDir(conf.FileLedger.Prefix)
56+
}
57+
logger.Debug("Ledger dir:", ld)
58+
lf = jsonledger.New(ld)
59+
case "ram":
60+
fallthrough
61+
default:
62+
lf = ramledger.New(int(conf.RAMLedger.HistorySize))
63+
}
64+
return lf, ld
65+
}
66+
67+
func createTempDir(dirPrefix string) string {
68+
dirPath, err := ioutil.TempDir("", dirPrefix)
69+
if err != nil {
70+
logger.Panic("Error creating temp dir:", err)
71+
}
72+
return dirPath
73+
}
74+
75+
func createSubDir(parentDirPath string, subDir string) (string, bool) {
76+
var created bool
77+
subDirPath := filepath.Join(parentDirPath, subDir)
78+
if _, err := os.Stat(subDirPath); err != nil {
79+
if os.IsNotExist(err) {
80+
if err = os.Mkdir(subDirPath, 0755); err != nil {
81+
logger.Panic("Error creating sub dir:", err)
82+
}
83+
created = true
84+
} else {
85+
logger.Debugf("Found %s sub-dir and using it", fsblkstorage.ChainsDir)
86+
}
87+
}
88+
return subDirPath, created
89+
}
90+
91+
// XXX The functions below need to be moved to the SBFT package ASAP
92+
93+
func makeSbftConsensusConfig(conf *config.TopLevel) *sbft.ConsensusConfig {
94+
cfg := simplebft.Config{N: conf.Genesis.SbftShared.N, F: conf.Genesis.SbftShared.F,
95+
BatchDurationNsec: uint64(conf.Genesis.DeprecatedBatchTimeout),
96+
BatchSizeBytes: uint64(conf.Genesis.DeprecatedBatchSize),
97+
RequestTimeoutNsec: conf.Genesis.SbftShared.RequestTimeoutNsec}
98+
peers := make(map[string][]byte)
99+
for addr, cert := range conf.Genesis.SbftShared.Peers {
100+
peers[addr], _ = sbftcrypto.ParseCertPEM(cert)
101+
}
102+
return &sbft.ConsensusConfig{Consensus: &cfg, Peers: peers}
103+
}
104+
105+
func makeSbftStackConfig(conf *config.TopLevel) *backend.StackConfig {
106+
return &backend.StackConfig{ListenAddr: conf.SbftLocal.PeerCommAddr,
107+
CertFile: conf.SbftLocal.CertFile,
108+
KeyFile: conf.SbftLocal.KeyFile,
109+
DataDir: conf.SbftLocal.DataDir}
110+
}

orderer/util_test.go

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"os"
21+
"testing"
22+
23+
config "github.com/hyperledger/fabric/orderer/localconfig"
24+
)
25+
26+
func TestCreateLedgerFactory(t *testing.T) {
27+
testCases := []struct {
28+
name string
29+
ledgerType string
30+
ledgerDir string
31+
ledgerDirPrefix string
32+
expectPanic bool
33+
}{
34+
{"RAM", "ram", "", "", false},
35+
{"JSONwithPathSet", "json", "test-dir", "", false},
36+
{"JSONwithPathUnset", "json", "", "test-prefix", false},
37+
{"FilewithPathSet", "file", "test-dir", "", false},
38+
{"FilewithPathUnset", "file", "", "test-prefix", false},
39+
}
40+
41+
conf := config.Load()
42+
43+
for _, tc := range testCases {
44+
t.Run(tc.name, func(t *testing.T) {
45+
defer func() {
46+
r := recover()
47+
if tc.expectPanic && r == nil {
48+
t.Fatal("Should have panicked")
49+
}
50+
if !tc.expectPanic && r != nil {
51+
t.Fatal("Should not have panicked")
52+
}
53+
}()
54+
55+
conf.General.LedgerType = tc.ledgerType
56+
conf.FileLedger.Location = tc.ledgerDir
57+
conf.FileLedger.Prefix = tc.ledgerDirPrefix
58+
lf, ld := createLedgerFactory(conf)
59+
60+
defer func() {
61+
if ld != "" {
62+
os.RemoveAll(ld)
63+
t.Log("Removed temp dir:", ld)
64+
}
65+
}()
66+
lf.ChainIDs()
67+
})
68+
}
69+
}
70+
71+
func TestCreateSubDir(t *testing.T) {
72+
testCases := []struct {
73+
name string
74+
count int
75+
expectCreated bool
76+
expectPanic bool
77+
}{
78+
{"CleanDir", 1, true, false},
79+
{"HasSubDir", 2, false, false},
80+
}
81+
82+
for _, tc := range testCases {
83+
t.Run(tc.name, func(t *testing.T) {
84+
defer func() {
85+
r := recover()
86+
if tc.expectPanic && r == nil {
87+
t.Fatal("Should have panicked")
88+
}
89+
if !tc.expectPanic && r != nil {
90+
t.Fatal("Should not have panicked")
91+
}
92+
}()
93+
94+
parentDirPath := createTempDir("test-dir")
95+
96+
var created bool
97+
for i := 0; i < tc.count; i++ {
98+
_, created = createSubDir(parentDirPath, "test-sub-dir")
99+
}
100+
101+
if created != tc.expectCreated {
102+
t.Fatalf("Sub dir created = %v, but expectation was = %v", created, tc.expectCreated)
103+
}
104+
})
105+
}
106+
}

0 commit comments

Comments
 (0)