Repository: dragonflyoss/Dragonfly2
Pull request #1180 (2022-03-23 07:09:05)

chore: always fallback to legacy get pieces
Signed-off-by: Jim Ma <majinjing3@gmail.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- Describe your changes in detail -->

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] Code compiles correctly.
- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.


codecov-commenter: # [Codecov](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1180?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) Report
> Merging [#1180](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1180?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (50d27c2) into [main](https://codecov.io/gh/dragonflyoss/Dragonfly2/commit/7d44095b7bde3a3a6973d48da25cb4edc95453cb?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (7d44095) will **increase** coverage by `0.56%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1180/graphs/tree.svg?width=650&height=150&src=pr&token=PUICJYSAT7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1180?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)

```diff
@@            Coverage Diff             @@
##             main    #1180      +/-   ##
==========================================
+ Coverage   46.04%   46.61%   +0.56%     
==========================================
  Files         133      133              
  Lines       10491    10498       +7     
==========================================
+ Hits         4831     4894      +63     
+ Misses       5114     5050      -64     
- Partials      546      554       +8     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `46.61% <100.00%> (+0.56%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1180?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) | Coverage Δ | |
|---|---|---|
| [...ent/daemon/peer/peertask\_piecetask\_synchronizer.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1180/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3BpZWNldGFza19zeW5jaHJvbml6ZXIuZ28=) | `56.70% <100.00%> (+9.79%)` | :arrow_up: |
| [client/daemon/peer/piece\_broker.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1180/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BpZWNlX2Jyb2tlci5nbw==) | `85.18% <0.00%> (-3.71%)` | :arrow_down: |
| [client/daemon/peer/peertask\_conductor.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1180/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX2NvbmR1Y3Rvci5nbw==) | `58.23% <0.00%> (+1.23%)` | :arrow_up: |
| [client/daemon/daemon.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1180/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9kYWVtb24uZ28=) | `6.54% <0.00%> (+1.41%)` | :arrow_up: |
| [client/daemon/peer/peertask\_piecetask\_poller.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1180/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3BpZWNldGFza19wb2xsZXIuZ28=) | `53.33% <0.00%> (+22.96%)` | :arrow_up: |


Patch:
diff --git a/client/daemon/daemon.go b/client/daemon/daemon.go
index 58a9679e..7d7c1b5a 100644
--- a/client/daemon/daemon.go
+++ b/client/daemon/daemon.go
@@ -447,26 +447,30 @@ func (cd *clientDaemon) Serve() error {
 
 	if cd.Option.AliveTime.Duration > 0 {
 		g.Go(func() error {
-			select {
-			case <-time.After(cd.Option.AliveTime.Duration):
-				var keepalives = []clientutil.KeepAlive{
-					cd.StorageManager,
-					cd.RPCManager,
-				}
-				var keep bool
-				for _, keepalive := range keepalives {
-					if keepalive.Alive(cd.Option.AliveTime.Duration) {
-						keep = true
+			for {
+				select {
+				case <-time.After(cd.Option.AliveTime.Duration):
+					var keepalives = []clientutil.KeepAlive{
+						cd.StorageManager,
+						cd.RPCManager,
 					}
+					var keep bool
+					for _, keepalive := range keepalives {
+						if keepalive.Alive(cd.Option.AliveTime.Duration) {
+							keep = true
+							break
+						}
+					}
+					if !keep {
+						cd.Stop()
+						logger.Infof("alive time reached, stop daemon")
+						return nil
+					}
+				case <-cd.done:
+					logger.Infof("peer host done, stop watch alive time")
+					return nil
 				}
-				if !keep {
-					cd.Stop()
-					logger.Infof("alive time reached, stop daemon")
-				}
-			case <-cd.done:
-				logger.Infof("peer host done, stop watch alive time")
 			}
-			return nil
 		})
 	}
 
diff --git a/client/daemon/peer/peertask_piecetask_synchronizer.go b/client/daemon/peer/peertask_piecetask_synchronizer.go
index a0b76d16..e8699752 100644
--- a/client/daemon/peer/peertask_piecetask_synchronizer.go
+++ b/client/daemon/peer/peertask_piecetask_synchronizer.go
@@ -125,6 +125,7 @@ func (s *pieceTaskSyncManager) newPieceTaskSynchronizer(
 	request.DstPid = dstPeer.PeerId
 	client, err := dfclient.SyncPieceTasks(ctx, dstPeer, request)
 	if err != nil {
+		s.peerTaskConductor.Errorf("call SyncPieceTasks error: %s, dest peer: %s", err, dstPeer.PeerId)
 		return err
 	}
 
@@ -132,6 +133,7 @@ func (s *pieceTaskSyncManager) newPieceTaskSynchronizer(
 	// when remove legacy get piece grpc, can move this check into synchronizer.receive
 	piecePacket, err := client.Recv()
 	if err != nil {
+		s.peerTaskConductor.Warnf("receive from SyncPieceTasksClient error: %s, dest peer: %s", err, dstPeer.PeerId)
 		_ = client.CloseSend()
 		return err
 	}
@@ -165,14 +167,14 @@ func (s *pieceTaskSyncManager) newMultiPieceTaskSynchronizer(
 		if err == nil {
 			continue
 		}
+		legacyPeers = append(legacyPeers, peer)
 		// when err is codes.Unimplemented, fallback to legacy get piece grpc
 		stat, ok := status.FromError(err)
 		if ok && stat.Code() == codes.Unimplemented {
-			legacyPeers = append(legacyPeers, peer)
 			s.peerTaskConductor.Warnf("connect peer %s error: %s, fallback to legacy get piece grpc", peer.PeerId, err)
 		} else {
 			s.reportError(peer)
-			s.peerTaskConductor.Errorf("connect peer %s error: %s, not codes.Unimplemented, did not fallback to legacy", peer.PeerId, err)
+			s.peerTaskConductor.Errorf("connect peer %s error: %s, not codes.Unimplemented", peer.PeerId, err)
 		}
 	}
 	s.cleanStaleWorker(destPeers)
diff --git a/manager/router/router.go b/manager/router/router.go
index 30de995b..c267b2f2 100644
--- a/manager/router/router.go
+++ b/manager/router/router.go
@@ -197,10 +197,10 @@ func Init(cfg *config.Config, logDir string, service service.Service, enforcer *
 
 	// Config
 	config := apiv1.Group("/configs")
-	config.POST("", h.CreateConfig)
-	config.DELETE(":id", h.DestroyConfig)
-	config.PATCH(":id", h.UpdateConfig)
-	config.GET(":id", h.GetConfig)
+	config.POST("", h.CreateConfig, jwt.MiddlewareFunc(), rbac)
+	config.DELETE(":id", h.DestroyConfig, jwt.MiddlewareFunc(), rbac)
+	config.PATCH(":id", h.UpdateConfig, jwt.MiddlewareFunc(), rbac)
+	config.GET(":id", h.GetConfig, jwt.MiddlewareFunc(), rbac)
 	config.GET("", h.GetConfigs)
 
 	// Job
diff --git a/manager/searcher/searcher.go b/manager/searcher/searcher.go
index aeb5bced..690f89fa 100644
--- a/manager/searcher/searcher.go
+++ b/manager/searcher/searcher.go
@@ -166,7 +166,7 @@ func FilterSchedulerClusters(conditions map[string]string, schedulerClusters []m
 		// scheduler cluster SecurityRules also exists,
 		// then security_domain and SecurityRules are equal to match.
 		for _, securityRule := range schedulerCluster.SecurityGroup.SecurityRules {
-			if securityRule.Domain == securityDomain {
+			if strings.EqualFold(securityRule.Domain, securityDomain) {
 				clusters = append(clusters, schedulerCluster)
 			}
 		}
@@ -203,7 +203,7 @@ func calculateIDCAffinityScore(dst, src string) float64 {
 		return minScore
 	}
 
-	if dst == src {
+	if strings.EqualFold(dst, src) {
 		return maxScore
 	}
 
@@ -212,7 +212,7 @@ func calculateIDCAffinityScore(dst, src string) float64 {
 	// it gets the max score of idc.
 	srcElements := strings.Split(src, "|")
 	for _, srcElement := range srcElements {
-		if dst == srcElement {
+		if strings.EqualFold(dst, srcElement) {
 			return maxScore
 		}
 	}
@@ -226,7 +226,7 @@ func calculateMultiElementAffinityScore(dst, src string) float64 {
 		return minScore
 	}
 
-	if dst == src {
+	if strings.EqualFold(dst, src) {
 		return maxScore
 	}
 
@@ -242,7 +242,7 @@ func calculateMultiElementAffinityScore(dst, src string) float64 {
 	}
 
 	for i := 0; i < elementLen; i++ {
-		if dstElements[i] != srcElements[i] {
+		if !strings.EqualFold(dstElements[i], srcElements[i]) {
 			break
 		}
 		score++


Test patch:
diff --git a/manager/searcher/searcher_test.go b/manager/searcher/searcher_test.go
index 70d892ce..a49bc5a6 100644
--- a/manager/searcher/searcher_test.go
+++ b/manager/searcher/searcher_test.go
@@ -616,6 +616,115 @@ func TestSchedulerCluster(t *testing.T) {
 				assert.Equal(len(data), 4)
 			},
 		},
+		{
+			name: "match according to all conditions with the case insensitive",
+			schedulerClusters: []model.SchedulerCluster{
+				{
+					Name: "foo",
+					Scopes: map[string]interface{}{
+						"idc":      "IDC-1",
+						"location": "LOCATION-2",
+					},
+					SecurityGroup: model.SecurityGroup{
+						SecurityRules: []model.SecurityRule{
+							{
+								Domain: "DOMAIN-1",
+							},
+						},
+					},
+					Schedulers: []model.Scheduler{
+						{
+							HostName: "foo",
+							State:    "active",
+						},
+					},
+				},
+				{
+					Name: "bar",
+					Scopes: map[string]interface{}{
+						"idc":      "IDC-1",
+						"location": "LOCATION-1",
+					},
+					SecurityGroup: model.SecurityGroup{
+						SecurityRules: []model.SecurityRule{
+							{
+								Domain: "DOMAIN-1",
+							},
+						},
+					},
+					Schedulers: []model.Scheduler{
+						{
+							HostName: "bar",
+							State:    "active",
+						},
+					},
+				},
+				{
+					Name: "baz",
+					Scopes: map[string]interface{}{
+						"idc":          "IDC-1",
+						"location":     "LOCATION-1|LOCATION-2",
+						"net_topology": "NET_TOPOLOGY-1",
+					},
+					Schedulers: []model.Scheduler{
+						{
+							HostName: "baz",
+							State:    "active",
+						},
+					},
+				},
+				{
+					Name: "bax",
+					Scopes: map[string]interface{}{
+						"idc":          "IDC-1",
+						"location":     "LOCATION-2",
+						"net_topology": "NET_TOPOLOGY-1|NET_TOPOLOGY-2",
+					},
+					Schedulers: []model.Scheduler{
+						{
+							HostName: "bax",
+							State:    "active",
+						},
+					},
+					IsDefault: true,
+				},
+				{
+					Name: "bac",
+					Scopes: map[string]interface{}{
+						"idc":          "IDC-1",
+						"location":     "LOCATION-2",
+						"net_topology": "NET_TOPOLOGY-1|NET_TOPOLOGY-2",
+					},
+					SecurityGroup: model.SecurityGroup{
+						SecurityRules: []model.SecurityRule{
+							{
+								Domain: "DOMAIN-2",
+							},
+						},
+					},
+					Schedulers: []model.Scheduler{
+						{
+							HostName: "bac",
+							State:    "active",
+						},
+					},
+				},
+			},
+			conditions: map[string]string{
+				"security_domain": "domain-1",
+				"idc":             "idc-1|idc-2",
+				"location":        "location-1|location-2",
+				"net_topology":    "net_topology-1|net_topology-1",
+			},
+			expect: func(t *testing.T, data []model.SchedulerCluster, err error) {
+				assert := assert.New(t)
+				assert.Equal(data[0].Name, "bar")
+				assert.Equal(data[1].Name, "foo")
+				assert.Equal(data[2].Name, "baz")
+				assert.Equal(data[3].Name, "bax")
+				assert.Equal(len(data), 4)
+			},
+		},
 	}
 
 	for _, tc := range tests {