From a5a6fc287add6ba987b81319ea2d78198fa0a7b9 Mon Sep 17 00:00:00 2001 From: Alain Jobart Date: Wed, 13 Dec 2017 13:24:10 -0800 Subject: [PATCH] Creating ZK higher level directories. It seems it's just simpler all around if the zk topology plugin creates the higher level directories when needed. The deletion still stops at the root though, seems safer. --- doc/TopologyService.md | 5 +++-- examples/local/zk-up.sh | 6 +----- go/vt/topo/zk2topo/file.go | 5 +---- go/vt/topo/zk2topo/server_test.go | 13 +------------ go/vt/topo/zk2topo/utils.go | 7 ++++--- test/topo_flavor/zk2.py | 12 +++--------- 6 files changed, 13 insertions(+), 35 deletions(-) diff --git a/doc/TopologyService.md b/doc/TopologyService.md index b4516faa8e..44693b3d31 100644 --- a/doc/TopologyService.md +++ b/doc/TopologyService.md @@ -264,8 +264,9 @@ and by the root directory to put the Vitess data in. For instance, assuming we want to use servers `global_server1,global_server2` in path `/vitess/global`: ``` sh -# First create the directory in the global server: -zk -server global_server1,global_server2 touch -p /vitess/global +# The root directory in the global server will be created +# automatically, same as when running this command: +# zk -server global_server1,global_server2 touch -p /vitess/global # Set the following flags to let Vitess use this global server: # -topo_implementation zk2 diff --git a/examples/local/zk-up.sh b/examples/local/zk-up.sh index 672f693780..086b32e71d 100755 --- a/examples/local/zk-up.sh +++ b/examples/local/zk-up.sh @@ -50,11 +50,7 @@ done echo "Started zk servers." -# Now create the toplevel directories we'll need, if not there. -$VTROOT/bin/zk -server $ZK_SERVER touch -p /vitess/global -$VTROOT/bin/zk -server $ZK_SERVER touch -p /vitess/test - -# And also add the CellInfo description for the 'test' cell. +# Add the CellInfo description for the 'test' cell. # If the node already exists, it's fine, means we used existing data. $VTROOT/bin/vtctl $TOPOLOGY_FLAGS AddCellInfo \ -root /vitess/test \ diff --git a/go/vt/topo/zk2topo/file.go b/go/vt/topo/zk2topo/file.go index d38c1386a4..4146b6dc43 100644 --- a/go/vt/topo/zk2topo/file.go +++ b/go/vt/topo/zk2topo/file.go @@ -20,7 +20,6 @@ import ( "bytes" "fmt" "path" - "strings" "github.com/samuel/go-zookeeper/zk" "golang.org/x/net/context" @@ -30,11 +29,9 @@ import ( // Create is part of the topo.Conn interface. func (zs *Server) Create(ctx context.Context, filePath string, contents []byte) (topo.Version, error) { - filePath = path.Clean(filePath) - depth := strings.Count(filePath, "/") zkPath := path.Join(zs.root, filePath) - pathCreated, err := CreateRecursive(ctx, zs.conn, zkPath, contents, 0, zk.WorldACL(PermFile), depth) + pathCreated, err := CreateRecursive(ctx, zs.conn, zkPath, contents, 0, zk.WorldACL(PermFile), -1) if err != nil { return nil, convertError(err) } diff --git a/go/vt/topo/zk2topo/server_test.go b/go/vt/topo/zk2topo/server_test.go index 210d708c1b..05dd63bff0 100644 --- a/go/vt/topo/zk2topo/server_test.go +++ b/go/vt/topo/zk2topo/server_test.go @@ -21,7 +21,6 @@ import ( "path" "testing" - "github.com/samuel/go-zookeeper/zk" "golang.org/x/net/context" "github.com/youtube/vitess/go/testfiles" @@ -41,22 +40,12 @@ func TestZk2Topo(t *testing.T) { testIndex := 0 test.TopoServerTestSuite(t, func() *topo.Server { // Each test will use its own sub-directories. + // The directories will be created when used the first time. testRoot := fmt.Sprintf("/test-%v", testIndex) testIndex++ - ctx := context.Background() - c := Connect(serverAddr) - if _, err := c.Create(ctx, testRoot, nil, 0, zk.WorldACL(PermDirectory)); err != nil { - t.Fatalf("Create(%v) failed: %v", testRoot, err) - } globalRoot := path.Join(testRoot, topo.GlobalCell) - if _, err := c.Create(ctx, globalRoot, nil, 0, zk.WorldACL(PermDirectory)); err != nil { - t.Fatalf("Create(%v) failed: %v", globalRoot, err) - } cellRoot := path.Join(testRoot, test.LocalCellName) - if _, err := c.Create(ctx, cellRoot, nil, 0, zk.WorldACL(PermDirectory)); err != nil { - t.Fatalf("Create(%v) failed: %v", cellRoot, err) - } // Note we exercise the observer feature here by passing in // the same server twice, with a "|" separator. diff --git a/go/vt/topo/zk2topo/utils.go b/go/vt/topo/zk2topo/utils.go index dad252f92a..b8261c11ec 100644 --- a/go/vt/topo/zk2topo/utils.go +++ b/go/vt/topo/zk2topo/utils.go @@ -33,8 +33,9 @@ import ( // CreateRecursive is a helper function on top of Create. It will // create a path and any pieces required, think mkdir -p. // Intermediate znodes are always created empty. -func CreateRecursive(ctx context.Context, conn Conn, zkPath string, value []byte, flags int32, aclv []zk.ACL, maxCreationDepth int) (pathCreated string, err error) { - pathCreated, err = conn.Create(ctx, zkPath, value, flags, aclv) +// Pass maxCreationDepth=-1 to create all nodes to the top. +func CreateRecursive(ctx context.Context, conn Conn, zkPath string, value []byte, flags int32, aclv []zk.ACL, maxCreationDepth int) (string, error) { + pathCreated, err := conn.Create(ctx, zkPath, value, flags, aclv) if err == zk.ErrNoNode { if maxCreationDepth == 0 { return "", zk.ErrNoNode @@ -54,7 +55,7 @@ func CreateRecursive(ctx context.Context, conn Conn, zkPath string, value []byte } pathCreated, err = conn.Create(ctx, zkPath, value, flags, aclv) } - return + return pathCreated, err } // ChildrenRecursive returns the relative path of all the children of diff --git a/test/topo_flavor/zk2.py b/test/topo_flavor/zk2.py index d6926ccbd2..b460bcd8b5 100644 --- a/test/topo_flavor/zk2.py +++ b/test/topo_flavor/zk2.py @@ -1,13 +1,13 @@ #!/usr/bin/env python # Copyright 2017 Google Inc. -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -50,12 +50,6 @@ class Zk2TopoServer(server.TopoServer): '-zk.cfg', '1@%s:%s' % (self.hostname, self.zk_ports), 'init']) - # Create toplevel directories for global ZK, and one per cell. - run(binary_args('zk') + ['-server', self.addr, 'touch', '-p', '/global']) - run(binary_args('zk') + ['-server', self.addr, 'touch', '-p', '/test_nj']) - run(binary_args('zk') + ['-server', self.addr, 'touch', '-p', '/test_ny']) - run(binary_args('zk') + ['-server', self.addr, 'touch', '-p', '/test_ca']) - # Create the cell configurations using 'vtctl AddCellInfo' utils.run_vtctl_vtctl(['AddCellInfo', '-root', '/test_nj',