Skip to content

Commit 5eab91b

Browse files
authored
fix: snapshot file will be overwritten and mixed if multiple consumers or producers use different name server domains (apache#1099)
1 parent 4de354a commit 5eab91b

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

primitive/nsresolver.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package primitive
1818

1919
import (
20+
"crypto/md5"
21+
"encoding/hex"
2022
"fmt"
2123
"io/ioutil"
2224
"net/http"
@@ -139,7 +141,7 @@ func (h *HttpResolver) Resolve() []string {
139141
}
140142

141143
func (h *HttpResolver) Description() string {
142-
return fmt.Sprintf("passthrough resolver of domain:%v instance:%v", h.domain, h.instance)
144+
return fmt.Sprintf("http resolver of domain:%v", h.domain)
143145
}
144146

145147
func (h *HttpResolver) get() []string {
@@ -177,7 +179,7 @@ func (h *HttpResolver) get() []string {
177179
}
178180

179181
func (h *HttpResolver) saveSnapshot(body []byte) error {
180-
filePath := h.getSnapshotFilePath(h.instance)
182+
filePath := h.getSnapshotFilePath()
181183
err := ioutil.WriteFile(filePath, body, 0644)
182184
if err != nil {
183185
rlog.Error("name server snapshot save failed", map[string]interface{}{
@@ -194,7 +196,7 @@ func (h *HttpResolver) saveSnapshot(body []byte) error {
194196
}
195197

196198
func (h *HttpResolver) loadSnapshot() []string {
197-
filePath := h.getSnapshotFilePath(h.instance)
199+
filePath := h.getSnapshotFilePath()
198200
_, err := os.Stat(filePath)
199201
if os.IsNotExist(err) {
200202
rlog.Warning("name server snapshot local file not exists", map[string]interface{}{
@@ -214,7 +216,7 @@ func (h *HttpResolver) loadSnapshot() []string {
214216
return strings.Split(string(bs), ";")
215217
}
216218

217-
func (h *HttpResolver) getSnapshotFilePath(instanceName string) string {
219+
func (h *HttpResolver) getSnapshotFilePath() string {
218220
homeDir := ""
219221
if usr, err := user.Current(); err == nil {
220222
homeDir = usr.HomeDir
@@ -232,6 +234,8 @@ func (h *HttpResolver) getSnapshotFilePath(instanceName string) string {
232234
})
233235
}
234236
}
235-
filePath := path.Join(storePath, fmt.Sprintf("nameserver_addr-%s", instanceName))
237+
hash := md5.Sum([]byte(h.domain))
238+
domainHash := hex.EncodeToString(hash[:])
239+
filePath := path.Join(storePath, fmt.Sprintf("nameserver_addr-%s", domainHash))
236240
return filePath
237241
}

primitive/nsresolver_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestHttpResolverWithGet(t *testing.T) {
7575
resolver.Resolve()
7676

7777
// check snapshot saved
78-
filePath := resolver.getSnapshotFilePath("DEFAULT")
78+
filePath := resolver.getSnapshotFilePath()
7979
body := strings.Join(srvs, ";")
8080
bs, _ := ioutil.ReadFile(filePath)
8181
So(string(bs), ShouldEqual, body)
@@ -112,7 +112,7 @@ func TestHttpResolverWithGetUnitName(t *testing.T) {
112112
resolver.Resolve()
113113

114114
// check snapshot saved
115-
filePath := resolver.getSnapshotFilePath("DEFAULT")
115+
filePath := resolver.getSnapshotFilePath()
116116
body := strings.Join(srvs, ";")
117117
bs, _ := ioutil.ReadFile(filePath)
118118
So(string(bs), ShouldEqual, body)
@@ -133,7 +133,7 @@ func TestHttpResolverWithSnapshotFile(t *testing.T) {
133133

134134
os.Setenv("NAMESRV_ADDR", "") // clear env
135135
// setup local snapshot file
136-
filePath := resolver.getSnapshotFilePath("DEFAULT")
136+
filePath := resolver.getSnapshotFilePath()
137137
body := strings.Join(srvs, ";")
138138
_ = ioutil.WriteFile(filePath, []byte(body), 0644)
139139

@@ -143,7 +143,7 @@ func TestHttpResolverWithSnapshotFile(t *testing.T) {
143143
})
144144
}
145145

146-
func TesHttpReslverWithSnapshotFileOnce(t *testing.T) {
146+
func TestHttpResolverWithSnapshotFileOnce(t *testing.T) {
147147
Convey("Test UpdateNameServerAddress Load Local Snapshot Once", t, func() {
148148
srvs := []string{
149149
"192.168.100.1",
@@ -157,18 +157,18 @@ func TesHttpReslverWithSnapshotFileOnce(t *testing.T) {
157157

158158
os.Setenv("NAMESRV_ADDR", "") // clear env
159159
// setup local snapshot file
160-
filePath := resolver.getSnapshotFilePath("DEFAULT")
160+
filePath := resolver.getSnapshotFilePath()
161161
body := strings.Join(srvs, ";")
162162
_ = ioutil.WriteFile(filePath, []byte(body), 0644)
163163
// load local snapshot file first time
164164
addrs1 := resolver.Resolve()
165165

166-
// change the local snapshot file to check load once
166+
// change the local snapshot file
167167
_ = ioutil.WriteFile(filePath, []byte("127.0.0.1;127.0.0.2"), 0644)
168168

169169
addrs2 := resolver.Resolve()
170170

171-
So(Diff(addrs1, addrs2), ShouldBeFalse)
171+
So(Diff(addrs1, addrs2), ShouldBeTrue)
172172
So(Diff(addrs1, srvs), ShouldBeFalse)
173173
})
174174
}

0 commit comments

Comments
 (0)