util.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafthttp
  15. import (
  16. "encoding/binary"
  17. "fmt"
  18. "io"
  19. "net/http"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
  21. "github.com/coreos/etcd/raft/raftpb"
  22. "github.com/coreos/etcd/version"
  23. )
  24. func writeEntryTo(w io.Writer, ent *raftpb.Entry) error {
  25. size := ent.Size()
  26. if err := binary.Write(w, binary.BigEndian, uint64(size)); err != nil {
  27. return err
  28. }
  29. b, err := ent.Marshal()
  30. if err != nil {
  31. return err
  32. }
  33. _, err = w.Write(b)
  34. return err
  35. }
  36. func readEntryFrom(r io.Reader, ent *raftpb.Entry) error {
  37. var l uint64
  38. if err := binary.Read(r, binary.BigEndian, &l); err != nil {
  39. return err
  40. }
  41. buf := make([]byte, int(l))
  42. if _, err := io.ReadFull(r, buf); err != nil {
  43. return err
  44. }
  45. return ent.Unmarshal(buf)
  46. }
  47. // compareMajorMinorVersion returns an integer comparing two versions based on
  48. // their major and minor version. The result will be 0 if a==b, -1 if a < b,
  49. // and 1 if a > b.
  50. func compareMajorMinorVersion(a, b *semver.Version) int {
  51. na := &semver.Version{Major: a.Major, Minor: a.Minor}
  52. nb := &semver.Version{Major: b.Major, Minor: b.Minor}
  53. switch {
  54. case na.LessThan(*nb):
  55. return -1
  56. case nb.LessThan(*na):
  57. return 1
  58. default:
  59. return 0
  60. }
  61. }
  62. // serverVersion returns the server version from the given header.
  63. func serverVersion(h http.Header) *semver.Version {
  64. verStr := h.Get("X-Server-Version")
  65. // backward compatibility with etcd 2.0
  66. if verStr == "" {
  67. verStr = "2.0.0"
  68. }
  69. return semver.Must(semver.NewVersion(verStr))
  70. }
  71. // serverVersion returns the min cluster version from the given header.
  72. func minClusterVersion(h http.Header) *semver.Version {
  73. verStr := h.Get("X-Min-Cluster-Version")
  74. // backward compatibility with etcd 2.0
  75. if verStr == "" {
  76. verStr = "2.0.0"
  77. }
  78. return semver.Must(semver.NewVersion(verStr))
  79. }
  80. // checkVersionCompability checks whether the given version is compatible
  81. // with the local version.
  82. func checkVersionCompability(name string, server, minCluster *semver.Version) error {
  83. localServer := semver.Must(semver.NewVersion(version.Version))
  84. localMinCluster := semver.Must(semver.NewVersion(version.MinClusterVersion))
  85. if compareMajorMinorVersion(server, localMinCluster) == -1 {
  86. return fmt.Errorf("remote version is too low: remote[%s]=%s, local=%s", name, server, localServer)
  87. }
  88. if compareMajorMinorVersion(minCluster, localServer) == 1 {
  89. return fmt.Errorf("local version is too low: remote[%s]=%s, local=%s", name, server, localServer)
  90. }
  91. return nil
  92. }