Skip to content

Commit

Permalink
Merge pull request hyperledger#1 from ThienLK1/v.2.2-mongodb
Browse files Browse the repository at this point in the history
V.2.2 mongodb
  • Loading branch information
ThienLK1 authored Nov 18, 2020
2 parents b026fd4 + 092bac7 commit f6fb384
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ tags
TESTS*.xml
.tox/
.vagrant/
.vscode
.vscode
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,16 @@ $(BUILD_DIR)/images/peer/$(DUMMY): BUILD_ARGS=--build-arg GO_TAGS=${GO_TAGS}
$(BUILD_DIR)/images/orderer/$(DUMMY): BUILD_ARGS=--build-arg GO_TAGS=${GO_TAGS}

$(BUILD_DIR)/images/%/$(DUMMY):
@echo "Building Docker image $(DOCKER_NS)/fabric-$*"
@echo "Building Docker image $(DOCKER_NS_CUSTOM)/fabric-$*"
@mkdir -p $(@D)
$(DBUILD) -f images/$*/Dockerfile \
--build-arg GO_VER=$(GO_VER) \
--build-arg ALPINE_VER=$(ALPINE_VER) \
$(BUILD_ARGS) \
-t $(DOCKER_NS)/fabric-$* ./$(BUILD_CONTEXT)
docker tag $(DOCKER_NS)/fabric-$* $(DOCKER_NS)/fabric-$*:$(BASE_VERSION)
docker tag $(DOCKER_NS)/fabric-$* $(DOCKER_NS)/fabric-$*:$(TWO_DIGIT_VERSION)
docker tag $(DOCKER_NS)/fabric-$* $(DOCKER_NS)/fabric-$*:$(DOCKER_TAG)
-t $(DOCKER_NS_CUSTOM)/fabric-$* ./$(BUILD_CONTEXT)
docker tag $(DOCKER_NS_CUSTOM)/fabric-$* $(DOCKER_NS_CUSTOM)/fabric-$*:$(BASE_VERSION)
docker tag $(DOCKER_NS_CUSTOM)/fabric-$* $(DOCKER_NS_CUSTOM)/fabric-$*:$(TWO_DIGIT_VERSION)
docker tag $(DOCKER_NS_CUSTOM)/fabric-$* $(DOCKER_NS_CUSTOM)/fabric-$*:$(DOCKER_TAG)
@touch $@

# builds release packages for the host platform
Expand Down Expand Up @@ -268,31 +268,31 @@ dist/%: release/%
.PHONY: docker-list
docker-list: $(RELEASE_IMAGES:%=%-docker-list)
%-docker-list:
@echo $(DOCKER_NS)/fabric-$*:$(DOCKER_TAG)
@echo $(DOCKER_NS_CUSTOM)/fabric-$*:$(DOCKER_TAG)

.PHONY: docker-clean
docker-clean: $(RELEASE_IMAGES:%=%-docker-clean)
%-docker-clean:
-@for image in "$$(docker images --quiet --filter=reference='$(DOCKER_NS)/fabric-$*:$(DOCKER_TAG)')"; do \
-@for image in "$$(docker images --quiet --filter=reference='$(DOCKER_NS_CUSTOM)/fabric-$*:$(DOCKER_TAG)')"; do \
[ -z "$$image" ] || docker rmi -f $$image; \
done
-@rm -rf $(BUILD_DIR)/images/$* || true

.PHONY: docker-tag-latest
docker-tag-latest: $(RELEASE_IMAGES:%=%-docker-tag-latest)
%-docker-tag-latest:
docker tag $(DOCKER_NS)/fabric-$*:$(DOCKER_TAG) $(DOCKER_NS)/fabric-$*:latest
docker tag $(DOCKER_NS_CUSTOM)/fabric-$*:$(DOCKER_TAG) $(DOCKER_NS_CUSTOM)/fabric-$*:latest

.PHONY: docker-tag-stable
docker-tag-stable: $(RELEASE_IMAGES:%=%-docker-tag-stable)
%-docker-tag-stable:
docker tag $(DOCKER_NS)/fabric-$*:$(DOCKER_TAG) $(DOCKER_NS)/fabric-$*:stable
docker tag $(DOCKER_NS_CUSTOM)/fabric-$*:$(DOCKER_TAG) $(DOCKER_NS_CUSTOM)/fabric-$*:stable

.PHONY: publish-images
publish-images: $(RELEASE_IMAGES:%=%-publish-images)
%-publish-images:
@docker login $(DOCKER_HUB_USERNAME) $(DOCKER_HUB_PASSWORD)
@docker push $(DOCKER_NS)/fabric-$*:$(PROJECT_VERSION)
@docker push $(DOCKER_NS_CUSTOM)/fabric-$*:$(PROJECT_VERSION)

.PHONY: clean
clean: docker-clean unit-test-clean release-clean
Expand Down
2 changes: 1 addition & 1 deletion core/chaincode/chaincodetest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ chaincode:

golang:
# golang will never need more than baseos
runtime: $(DOCKER_NS)/fabric-baseos:$(TWO_DIGIT_VERSION)
runtime: hyperledger/fabric-baseos:$(TWO_DIGIT_VERSION)

# whether or not golang chaincode should be linked dynamically
dynamicLink: false
Expand Down
8 changes: 5 additions & 3 deletions core/ledger/kvledger/txmgmt/privacyenabledstate/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/hyperledger/fabric/core/ledger/kvledger/bookkeeping"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statemongodb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/stateleveldb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statemongodb"
"github.com/hyperledger/fabric/core/ledger/util"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -65,7 +65,8 @@ func NewDBProvider(

var vdbProvider statedb.VersionedDBProvider
var err error
logger.Debugf("constructing NewDBProvider stateDBConf %s",stateDBConf.MongoDB)
logger.Debugf("****StateDB****: %s", stateDBConf.StateDatabase)
//logger.Debugf("constructing NewDBProvider stateDBConf %s",stateDBConf.MongoDB)

if stateDBConf != nil && stateDBConf.StateDatabase == couchDB {
if vdbProvider, err = statecouchdb.NewVersionedDBProvider(stateDBConf.CouchDB, metricsProvider, sysNamespaces); err != nil {
Expand Down Expand Up @@ -204,6 +205,7 @@ func (s *DB) GetPrivateDataHash(namespace, collection, key string) (*statedb.Ver

// GetPrivateDataHash gets the value hash of a private data item identified by a tuple <namespace, collection, keyHash>
func (s *DB) GetValueHash(namespace, collection string, keyHash []byte) (*statedb.VersionedValue, error) {
logger.Debugf("GetValueHash")
keyHashStr := string(keyHash)
if !s.BytesKeySupported() {
keyHashStr = base64.StdEncoding.EncodeToString(keyHash)
Expand Down Expand Up @@ -263,7 +265,7 @@ func (s *DB) ApplyPrivacyAwareUpdates(updates *UpdateBatch, height *version.Heig
addPvtUpdates(combinedUpdates, updates.PvtUpdates)
addHashedUpdates(combinedUpdates, updates.HashUpdates, !s.BytesKeySupported())
s.metadataHint.setMetadataUsedFlag(updates)
logger.Debugf("ApplyPrivacyAwareUpdates %s",s.VersionedDB)
logger.Debugf("ApplyPrivacyAwareUpdates %s", s.VersionedDB)
return s.VersionedDB.ApplyUpdates(combinedUpdates.UpdateBatch, height)
}

Expand Down
34 changes: 19 additions & 15 deletions core/ledger/kvledger/txmgmt/statedb/statecouchdb/couchdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func (dbclient *couchDatabase) dropDatabase() (*dbOperationResponse, error) {
func (dbclient *couchDatabase) saveDoc(id string, rev string, couchDoc *couchDoc) (string, error) {
dbName := dbclient.dbName

couchdbLogger.Debugf("[%s] Entering SaveDoc() id=[%s]", dbName, id)
couchdbLogger.Debugf("[%s] Entering SaveDoc() id=[%s] couchDoc: %+v", dbName, id, couchDoc)

if !utf8.ValidString(id) {
return "", errors.Errorf("doc id [%x] not a valid utf8 string", id)
Expand All @@ -540,16 +540,17 @@ func (dbclient *couchDatabase) saveDoc(id string, rev string, couchDoc *couchDoc

//check to see if attachments is nil, if so, then this is a JSON only
if couchDoc.attachments == nil {

logger.Debugf("saveDoc() id: %s couchDoc.attachments == nil", id)
//Test to see if this is a valid JSON
if !isJSON(string(couchDoc.jsonValue)) {
return "", errors.New("JSON format is not valid")
}

// if there are no attachments, then use the bytes passed in as the JSON
data = couchDoc.jsonValue

logger.Debugf("saveDoc() data: %+v", data)
} else { // there are attachments
logger.Debugf("saveDoc() id: %s couchDoc.attachments != nil", id)

//attachments are included, create the multipart definition
multipartData, multipartBoundary, err3 := createAttachmentPart(couchDoc)
Expand All @@ -566,15 +567,15 @@ func (dbclient *couchDatabase) saveDoc(id string, rev string, couchDoc *couchDoc

//Set the data buffer to the data from the create multi-part data
data = multipartData.Bytes()

logger.Debugf("saveDoc() data: %+v", data)
//Set the default boundary to the value generated in the multipart creation
defaultBoundary = multipartBoundary

}

//get the number of retries
maxRetries := dbclient.couchInstance.conf.MaxRetries

//couchdbLogger.Debugf("[SaveDoc() id:[%s] rev=[%s]",id, rev)
//handle the request for saving document with a retry if there is a revision conflict
resp, _, err := dbclient.handleRequestWithRevisionRetry(id, http.MethodPut, dbName, "SaveDoc", saveURL, data, rev, defaultBoundary, maxRetries, keepConnectionOpen, nil)

Expand All @@ -589,8 +590,8 @@ func (dbclient *couchDatabase) saveDoc(id string, rev string, couchDoc *couchDoc
return "", err
}

couchdbLogger.Debugf("[%s] Exiting SaveDoc()", dbclient.dbName)

//couchdbLogger.Debugf("SaveDoc() %+v",revision)
couchdbLogger.Debugf("Database Name : [%s] Revision: [%s] Exiting saveDoc()", dbclient.dbName, revision)
return revision, nil

}
Expand All @@ -599,13 +600,14 @@ func (dbclient *couchDatabase) saveDoc(id string, rev string, couchDoc *couchDoc
func (dbclient *couchDatabase) getDocumentRevision(id string) string {

var rev = ""

logger.Debugf("getDocumentRevision")
//See if the document already exists, we need the rev for saves and deletes
_, revdoc, err := dbclient.readDoc(id)
if err == nil {
//set the revision to the rev returned from the document read
rev = revdoc
}
logger.Debugf("getDocumentRevision rev: %v", rev)
return rev
}

Expand Down Expand Up @@ -825,7 +827,7 @@ func (dbclient *couchDatabase) readDoc(id string) (*couchDoc, string, error) {
} // for all multiparts

couchDoc.attachments = attachments

couchdbLogger.Debugf("return readDoc couchDoc: %+v, revision: %s, nil", couchDoc, revision)
return &couchDoc, revision, nil
}

Expand All @@ -835,7 +837,7 @@ func (dbclient *couchDatabase) readDoc(id string) (*couchDoc, string, error) {
return nil, "", errors.Wrap(err, "error reading response body")
}

couchdbLogger.Debugf("[%s] Exiting ReadDoc()", dbclient.dbName)
couchdbLogger.Debugf("[%s] Exiting ReadDoc() id: %v revision: %v", dbclient.dbName, id, revision)
return &couchDoc, revision, nil
}

Expand Down Expand Up @@ -1456,11 +1458,12 @@ func (dbclient *couchDatabase) batchRetrieveDocumentMetadata(keys []string) ([]*
if err2 != nil {
return nil, errors.Wrap(err2, "error unmarshalling json data")
}

couchdbLogger.Debugf("BatchRetrieveDocumentMetadata() jsonResponse:%+v", jsonResponse)
docMetadataArray := []*docMetadata{}

for _, row := range jsonResponse.Rows {
docMetadata := &docMetadata{ID: row.ID, Rev: row.DocMetadata.Rev, Version: row.DocMetadata.Version}
couchdbLogger.Debugf("BatchRetrieveDocumentMetadata() docMetadata:%+v", docMetadata)
docMetadataArray = append(docMetadataArray, docMetadata)
}

Expand All @@ -1473,7 +1476,7 @@ func (dbclient *couchDatabase) batchRetrieveDocumentMetadata(keys []string) ([]*
//batchUpdateDocuments - batch method to batch update documents
func (dbclient *couchDatabase) batchUpdateDocuments(documents []*couchDoc) ([]*batchUpdateResponse, error) {
dbName := dbclient.dbName

logger.Debugf("batchUpdateDocuments =[%v]", documents)
if couchdbLogger.IsEnabledFor(zapcore.DebugLevel) {
documentIdsString, err := printDocumentIds(documents)
if err == nil {
Expand All @@ -1494,7 +1497,7 @@ func (dbclient *couchDatabase) batchUpdateDocuments(documents []*couchDoc) ([]*b
var jsonDocumentMap []interface{}

for _, jsonDocument := range documents {

couchdbLogger.Debugf("BatchUpdateDocuments() jsonDocument=[%+v]", jsonDocument)
//create a document map
var document = make(map[string]interface{})

Expand All @@ -1503,9 +1506,10 @@ func (dbclient *couchDatabase) batchUpdateDocuments(documents []*couchDoc) ([]*b
if err != nil {
return nil, errors.Wrap(err, "error unmarshalling json data")
}

couchdbLogger.Debugf("BatchUpdateDocuments() Unmarshal document: %+v", document)
//iterate through any attachments
if len(jsonDocument.attachments) > 0 {
couchdbLogger.Debugf("BatchUpdateDocuments() len(jsonDocument.attachments) > 0")

//create a file attachment map
fileAttachment := make(map[string]interface{})
Expand All @@ -1526,7 +1530,7 @@ func (dbclient *couchDatabase) batchUpdateDocuments(documents []*couchDoc) ([]*b
jsonDocumentMap = append(jsonDocumentMap, document)

}

couchdbLogger.Debugf("BatchUpdateDocuments() jsonDocumentMap=[%+v]", jsonDocumentMap)
//Add the documents to the "docs" item
documentMap["docs"] = jsonDocumentMap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func (c *committer) commitUpdates() error {
// IF INDIVIDUAL DOCUMENTS IN THE BULK UPDATE DID NOT SUCCEED, TRY THEM INDIVIDUALLY
// iterate through the response from MongoDB by document
for _, resp := range responses {
logger.Debugf("commitUpdates resp.Error :%s", resp.Error)
// If the document returned an error, retry the individual document
if resp.Error != "" || resp.ID == "" {
rev := string(resp.Rev)
Expand Down
Loading

0 comments on commit f6fb384

Please sign in to comment.