Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Upgrade): Users can now upgrade to v20.07.0 (#5634) #5830

Merged
merged 1 commit into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions upgrade/change_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2020 Dgraph Labs, Inc. and Contributors
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package upgrade

func init() {
allChanges = changeList{
{
introducedIn: &version{major: 20, minor: 3, patch: 0},
changes: []*change{{
name: "Upgrade ACL Rules",
description: "This updates the ACL nodes to use the new ACL model introduced in" +
" v20.03.0. This is applied while upgrading from v1.2.2+ to v20.03.0+",
minFromVersion: &version{major: 1, minor: 2, patch: 2},
applyFunc: upgradeACLRules,
}},
},
{
introducedIn: &version{major: 20, minor: 7, patch: 0},
changes: []*change{
{
name: "Upgrade ACL Type names",
description: "This updates the ACL nodes to use the new type names for the" +
" types User, Group and Rule. They are now dgraph.type.User, " +
"dgraph.type.Group, and dgraph.type.Rule. This change was introduced in " +
"v20.07.0, and is applied while upgrading from v20.03.0+ to v20.07.0+. " +
"For more info, see: https://github.com/dgraph-io/dgraph/pull/5185",
minFromVersion: &version{major: 20, minor: 3, patch: 0},
applyFunc: upgradeAclTypeNames,
},
// another change in 20.07.0 version is if someone was having types/predicates in
// reserved namespace, then data should be migrated for such cases,
// to not use the reserved namespace. Migration is expected to be done by the user,
// and we will only provide a documentation mentioning the steps to be taken.
// This is not being automated because:
// 1. There is very less chance that someone was using the reserved namespace,
// so this automation may not be used at all.
// 2. The data migration can be huge, if needed to migrate. The automated code
// can't handle every scenario. So, it is best to let the user do it.
},
},
}
}
80 changes: 15 additions & 65 deletions upgrade/acl.go → upgrade/change_v20.03.0.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@
package upgrade

import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/dgraph-io/dgo/v200"
"github.com/dgraph-io/dgo/v200/protos/api"
"google.golang.org/grpc"
)

const (
oldACLQuery = `
queryACLGroupsBefore_v20_03_0 = `
{
rules(func: type(dgraph.type.Group)) {
rules(func: type(Group)) @filter(has(dgraph.group.acl)) {
uid
dgraph.group.acl
}
Expand All @@ -51,48 +47,26 @@ type rule struct {
type rules []rule

func upgradeACLRules() error {
alpha := Upgrade.Conf.GetString("alpha")
userName := Upgrade.Conf.GetString("user")
password := Upgrade.Conf.GetString("password")
deleteOld := Upgrade.Conf.GetBool("deleteOld")

// TODO(Aman): add TLS configuration.
conn, err := grpc.Dial(alpha, grpc.WithInsecure())
dg, conn, err := getDgoClient(true)
if err != nil {
return fmt.Errorf("unable to connect to Dgraph cluster: %w", err)
return fmt.Errorf("error getting dgo client: %w", err)
}
defer conn.Close()

dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

// login to cluster
if err := dg.Login(ctx, userName, password); err != nil {
return fmt.Errorf("unable to login to Dgraph cluster: %w", err)
}

ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := dg.NewReadOnlyTxn().Query(ctx, oldACLQuery)
if err != nil {
return fmt.Errorf("unable to query old ACL rules: %w", err)
}

data := make(map[string][]group)
if err := json.Unmarshal(resp.Json, &data); err != nil {
return fmt.Errorf("unable to unmarshal old ACLs: %w", err)
if err = getQueryResult(dg, queryACLGroupsBefore_v20_03_0, &data); err != nil {
return fmt.Errorf("error querying old ACL rules: %w", err)
}

groups, ok := data["rules"]
if !ok {
return fmt.Errorf("unable to parse ACLs: %v", string(resp.Json))
return fmt.Errorf("unable to parse ACLs: %v", data)
}

counter := 1
var nquads []*api.NQuad
for _, group := range groups {
if len(group.ACL) == 0 {
if group.ACL == "" {
continue
}

Expand All @@ -104,11 +78,8 @@ func upgradeACLRules() error {
for _, r := range rs {
newRuleStr := fmt.Sprintf("_:newrule%d", counter)
nquads = append(nquads, []*api.NQuad{
{
Subject: newRuleStr,
Predicate: "dgraph.type",
ObjectId: "dgraph.type.Rule",
},
// the name of the type was Rule in v20.03.0
getTypeNquad(newRuleStr, "Rule"),
{
Subject: newRuleStr,
Predicate: "dgraph.rule.predicate",
Expand Down Expand Up @@ -136,18 +107,18 @@ func upgradeACLRules() error {

// Nothing to do.
if len(nquads) == 0 {
return fmt.Errorf("no old rules found in the cluster")
fmt.Println("nothing to do: no old rules found in the cluster")
return nil
}

if err := mutateACL(dg, nquads); err != nil {
if err := mutateWithClient(dg, &api.Mutation{Set: nquads}); err != nil {
return fmt.Errorf("error upgrading ACL rules: %w", err)
}
fmt.Println("Successfully upgraded ACL rules.")

ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
deleteOld := Upgrade.Conf.GetBool("deleteOld")
if deleteOld {
err = dg.Alter(ctx, &api.Operation{
err = alterWithClient(dg, &api.Operation{
DropOp: api.Operation_ATTR,
DropValue: "dgraph.group.acl",
})
Expand All @@ -159,24 +130,3 @@ func upgradeACLRules() error {

return nil
}

func mutateACL(dg *dgo.Dgraph, nquads []*api.NQuad) error {
var err error
for i := 0; i < 3; i++ {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

_, err = dg.NewTxn().Mutate(ctx, &api.Mutation{
Set: nquads,
CommitNow: true,
})
if err != nil {
fmt.Printf("error in running mutation, retrying: %v\n", err)
continue
}

return nil
}

return err
}
Loading