Browse Source

clientv3: simplify V(4) logger with Lvl(4)

Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>
Gyu-Ho Lee 8 years ago
parent
commit
805bcc828c
2 changed files with 13 additions and 39 deletions
  1. 9 27
      clientv3/health_balancer.go
  2. 4 12
      clientv3/retry.go

+ 9 - 27
clientv3/health_balancer.go

@@ -158,34 +158,26 @@ func (b *healthBalancer) pinned() string {
 
 
 func (b *healthBalancer) hostPortError(hostPort string, err error) {
 func (b *healthBalancer) hostPortError(hostPort string, err error) {
 	if b.endpoint(hostPort) == "" {
 	if b.endpoint(hostPort) == "" {
-		if logger.V(4) {
-			logger.Infof("clientv3/balancer: %q is stale (skip marking as unhealthy on %q)", hostPort, err.Error())
-		}
+		logger.Lvl(4).Infof("clientv3/balancer: %q is stale (skip marking as unhealthy on %q)", hostPort, err.Error())
 		return
 		return
 	}
 	}
 
 
 	b.unhealthyMu.Lock()
 	b.unhealthyMu.Lock()
 	b.unhealthyHostPorts[hostPort] = time.Now()
 	b.unhealthyHostPorts[hostPort] = time.Now()
 	b.unhealthyMu.Unlock()
 	b.unhealthyMu.Unlock()
-	if logger.V(4) {
-		logger.Infof("clientv3/balancer: %q is marked unhealthy (%q)", hostPort, err.Error())
-	}
+	logger.Lvl(4).Infof("clientv3/balancer: %q is marked unhealthy (%q)", hostPort, err.Error())
 }
 }
 
 
 func (b *healthBalancer) removeUnhealthy(hostPort, msg string) {
 func (b *healthBalancer) removeUnhealthy(hostPort, msg string) {
 	if b.endpoint(hostPort) == "" {
 	if b.endpoint(hostPort) == "" {
-		if logger.V(4) {
-			logger.Infof("clientv3/balancer: %q was not in unhealthy (%q)", hostPort, msg)
-		}
+		logger.Lvl(4).Infof("clientv3/balancer: %q was not in unhealthy (%q)", hostPort, msg)
 		return
 		return
 	}
 	}
 
 
 	b.unhealthyMu.Lock()
 	b.unhealthyMu.Lock()
 	delete(b.unhealthyHostPorts, hostPort)
 	delete(b.unhealthyHostPorts, hostPort)
 	b.unhealthyMu.Unlock()
 	b.unhealthyMu.Unlock()
-	if logger.V(4) {
-		logger.Infof("clientv3/balancer: %q is removed from unhealthy (%q)", hostPort, msg)
-	}
+	logger.Lvl(4).Infof("clientv3/balancer: %q is removed from unhealthy (%q)", hostPort, msg)
 }
 }
 
 
 func (b *healthBalancer) countUnhealthy() (count int) {
 func (b *healthBalancer) countUnhealthy() (count int) {
@@ -207,9 +199,7 @@ func (b *healthBalancer) cleanupUnhealthy() {
 	for k, v := range b.unhealthyHostPorts {
 	for k, v := range b.unhealthyHostPorts {
 		if time.Since(v) > b.healthCheckTimeout {
 		if time.Since(v) > b.healthCheckTimeout {
 			delete(b.unhealthyHostPorts, k)
 			delete(b.unhealthyHostPorts, k)
-			if logger.V(4) {
-				logger.Infof("clientv3/balancer: removed %q from unhealthy after %v", k, b.healthCheckTimeout)
-			}
+			logger.Lvl(4).Infof("clientv3/balancer: removed %q from unhealthy after %v", k, b.healthCheckTimeout)
 		}
 		}
 	}
 	}
 	b.unhealthyMu.Unlock()
 	b.unhealthyMu.Unlock()
@@ -412,9 +402,7 @@ func (b *healthBalancer) Up(addr grpc.Address) func(error) {
 	}
 	}
 
 
 	if b.pinAddr != "" {
 	if b.pinAddr != "" {
-		if logger.V(4) {
-			logger.Infof("clientv3/balancer: %q is up but not pinned (already pinned %q)", addr.Addr, b.pinAddr)
-		}
+		logger.Lvl(4).Infof("clientv3/balancer: %q is up but not pinned (already pinned %q)", addr.Addr, b.pinAddr)
 		return func(err error) {}
 		return func(err error) {}
 	}
 	}
 
 
@@ -422,9 +410,7 @@ func (b *healthBalancer) Up(addr grpc.Address) func(error) {
 	close(b.upc)
 	close(b.upc)
 	b.downc = make(chan struct{})
 	b.downc = make(chan struct{})
 	b.pinAddr = addr.Addr
 	b.pinAddr = addr.Addr
-	if logger.V(4) {
-		logger.Infof("clientv3/balancer: pin %q", addr.Addr)
-	}
+	logger.Lvl(4).Infof("clientv3/balancer: pin %q", addr.Addr)
 
 
 	// notify client that a connection is up
 	// notify client that a connection is up
 	b.readyOnce.Do(func() { close(b.readyc) })
 	b.readyOnce.Do(func() { close(b.readyc) })
@@ -441,9 +427,7 @@ func (b *healthBalancer) Up(addr grpc.Address) func(error) {
 		close(b.downc)
 		close(b.downc)
 		b.pinAddr = ""
 		b.pinAddr = ""
 		b.mu.Unlock()
 		b.mu.Unlock()
-		if logger.V(4) {
-			logger.Infof("clientv3/balancer: unpin %q (%q)", addr.Addr, err.Error())
-		}
+		logger.Lvl(4).Infof("clientv3/balancer: unpin %q (%q)", addr.Addr, err.Error())
 	}
 	}
 }
 }
 
 
@@ -470,9 +454,7 @@ func (b *healthBalancer) mayPin(addr grpc.Address) bool {
 	//   3. grpc-healthcheck still SERVING, thus retry to pin
 	//   3. grpc-healthcheck still SERVING, thus retry to pin
 	// instead, return before grpc-healthcheck if failed within healthcheck timeout
 	// instead, return before grpc-healthcheck if failed within healthcheck timeout
 	if elapsed := time.Since(failedTime); elapsed < b.healthCheckTimeout {
 	if elapsed := time.Since(failedTime); elapsed < b.healthCheckTimeout {
-		if logger.V(4) {
-			logger.Infof("clientv3/balancer: %q is up but not pinned (failed %v ago, require minimum %v after failure)", addr.Addr, elapsed, b.healthCheckTimeout)
-		}
+		logger.Lvl(4).Infof("clientv3/balancer: %q is up but not pinned (failed %v ago, require minimum %v after failure)", addr.Addr, elapsed, b.healthCheckTimeout)
 		return false
 		return false
 	}
 	}
 
 

+ 4 - 12
clientv3/retry.go

@@ -60,17 +60,13 @@ func (c *Client) newRetryWrapper(isStop retryStopErrFunc) retryRPCFunc {
 			if err == nil {
 			if err == nil {
 				return nil
 				return nil
 			}
 			}
-			if logger.V(4) {
-				logger.Infof("clientv3/retry: error %q on pinned endpoint %q", err.Error(), pinned)
-			}
+			logger.Lvl(4).Infof("clientv3/retry: error %q on pinned endpoint %q", err.Error(), pinned)
 
 
 			if s, ok := status.FromError(err); ok && (s.Code() == codes.Unavailable || s.Code() == codes.DeadlineExceeded || s.Code() == codes.Internal) {
 			if s, ok := status.FromError(err); ok && (s.Code() == codes.Unavailable || s.Code() == codes.DeadlineExceeded || s.Code() == codes.Internal) {
 				// mark this before endpoint switch is triggered
 				// mark this before endpoint switch is triggered
 				c.balancer.hostPortError(pinned, err)
 				c.balancer.hostPortError(pinned, err)
 				c.balancer.next()
 				c.balancer.next()
-				if logger.V(4) {
-					logger.Infof("clientv3/retry: switching from %q due to error %q", pinned, err.Error())
-				}
+				logger.Lvl(4).Infof("clientv3/retry: switching from %q due to error %q", pinned, err.Error())
 			}
 			}
 
 
 			if isStop(err) {
 			if isStop(err) {
@@ -88,16 +84,12 @@ func (c *Client) newAuthRetryWrapper() retryRPCFunc {
 			if err == nil {
 			if err == nil {
 				return nil
 				return nil
 			}
 			}
-			if logger.V(4) {
-				logger.Infof("clientv3/auth-retry: error %q on pinned endpoint %q", err.Error(), pinned)
-			}
+			logger.Lvl(4).Infof("clientv3/auth-retry: error %q on pinned endpoint %q", err.Error(), pinned)
 			// always stop retry on etcd errors other than invalid auth token
 			// always stop retry on etcd errors other than invalid auth token
 			if rpctypes.Error(err) == rpctypes.ErrInvalidAuthToken {
 			if rpctypes.Error(err) == rpctypes.ErrInvalidAuthToken {
 				gterr := c.getToken(rpcCtx)
 				gterr := c.getToken(rpcCtx)
 				if gterr != nil {
 				if gterr != nil {
-					if logger.V(4) {
-						logger.Infof("clientv3/auth-retry: cannot retry due to error %q(%q) on pinned endpoint %q", err.Error(), gterr.Error(), pinned)
-					}
+					logger.Lvl(4).Infof("clientv3/auth-retry: cannot retry due to error %q(%q) on pinned endpoint %q", err.Error(), gterr.Error(), pinned)
 					return err // return the original error for simplicity
 					return err // return the original error for simplicity
 				}
 				}
 				continue
 				continue