| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package bigtable | 5 package bigtable |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "time" | 9 "time" |
| 10 | 10 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 } | 59 } |
| 60 | 60 |
| 61 // Initialize sets up a Storage schema in BigTable. If the schema is already | 61 // Initialize sets up a Storage schema in BigTable. If the schema is already |
| 62 // set up properly, no action will be taken. | 62 // set up properly, no action will be taken. |
| 63 // | 63 // |
| 64 // If, however, the table or table's schema doesn't exist, Initialize will | 64 // If, however, the table or table's schema doesn't exist, Initialize will |
| 65 // create and configure it. | 65 // create and configure it. |
| 66 // | 66 // |
| 67 // If nil is returned, the table is ready for use as a Storage via New. | 67 // If nil is returned, the table is ready for use as a Storage via New. |
| 68 func Initialize(ctx context.Context, o Options) error { | 68 func Initialize(ctx context.Context, o Options) error { |
| 69 » st := New(ctx, o) | 69 » adminClient, err := o.adminClient(ctx) |
| 70 » if err != nil { |
| 71 » » return err |
| 72 » } |
| 73 |
| 74 » st := newBTStorage(ctx, o, nil, adminClient) |
| 70 defer st.Close() | 75 defer st.Close() |
| 71 | 76 |
| 72 » c, err := st.(*btStorage).getAdminClient() | 77 » exists, err := tableExists(ctx, st.adminClient, o.LogTable) |
| 73 » if err != nil { | |
| 74 » » return fmt.Errorf("failed to create admin client: %s", err) | |
| 75 » } | |
| 76 | |
| 77 » exists, err := tableExists(ctx, c, o.LogTable) | |
| 78 if err != nil { | 78 if err != nil { |
| 79 return fmt.Errorf("failed to test for table: %s", err) | 79 return fmt.Errorf("failed to test for table: %s", err) |
| 80 } | 80 } |
| 81 if !exists { | 81 if !exists { |
| 82 log.Fields{ | 82 log.Fields{ |
| 83 "table": o.LogTable, | 83 "table": o.LogTable, |
| 84 }.Infof(ctx, "Storage table does not exist. Creating...") | 84 }.Infof(ctx, "Storage table does not exist. Creating...") |
| 85 | 85 |
| 86 » » if err := c.CreateTable(ctx, o.LogTable); err != nil { | 86 » » if err := st.adminClient.CreateTable(ctx, o.LogTable); err != ni
l { |
| 87 return fmt.Errorf("failed to create table: %s", err) | 87 return fmt.Errorf("failed to create table: %s", err) |
| 88 } | 88 } |
| 89 | 89 |
| 90 // Wait for the table to exist. BigTable API says this can be de
layed from | 90 // Wait for the table to exist. BigTable API says this can be de
layed from |
| 91 // creation. | 91 // creation. |
| 92 » » if err := waitForTable(ctx, c, o.LogTable); err != nil { | 92 » » if err := waitForTable(ctx, st.adminClient, o.LogTable); err !=
nil { |
| 93 return fmt.Errorf("failed to wait for table to exist: %s
", err) | 93 return fmt.Errorf("failed to wait for table to exist: %s
", err) |
| 94 } | 94 } |
| 95 | 95 |
| 96 log.Fields{ | 96 log.Fields{ |
| 97 "table": o.LogTable, | 97 "table": o.LogTable, |
| 98 }.Infof(ctx, "Successfully created storage table.") | 98 }.Infof(ctx, "Successfully created storage table.") |
| 99 } | 99 } |
| 100 | 100 |
| 101 // Get table info. | 101 // Get table info. |
| 102 » ti, err := c.TableInfo(ctx, o.LogTable) | 102 » ti, err := st.adminClient.TableInfo(ctx, o.LogTable) |
| 103 if err != nil { | 103 if err != nil { |
| 104 return fmt.Errorf("failed to get table info: %s", err) | 104 return fmt.Errorf("failed to get table info: %s", err) |
| 105 } | 105 } |
| 106 | 106 |
| 107 // The table must have the "log" column family. | 107 // The table must have the "log" column family. |
| 108 families := stringset.NewFromSlice(ti.Families...) | 108 families := stringset.NewFromSlice(ti.Families...) |
| 109 if !families.Has(logColumnFamily) { | 109 if !families.Has(logColumnFamily) { |
| 110 log.Fields{ | 110 log.Fields{ |
| 111 "table": o.LogTable, | 111 "table": o.LogTable, |
| 112 "family": logColumnFamily, | 112 "family": logColumnFamily, |
| 113 }.Infof(ctx, "Column family 'log' does not exist. Creating...") | 113 }.Infof(ctx, "Column family 'log' does not exist. Creating...") |
| 114 | 114 |
| 115 // Create the logColumnFamily column family. | 115 // Create the logColumnFamily column family. |
| 116 » » if err := c.CreateColumnFamily(ctx, o.LogTable, logColumnFamily)
; err != nil { | 116 » » if err := st.adminClient.CreateColumnFamily(ctx, o.LogTable, log
ColumnFamily); err != nil { |
| 117 return fmt.Errorf("Failed to create 'log' column family:
%s", err) | 117 return fmt.Errorf("Failed to create 'log' column family:
%s", err) |
| 118 } | 118 } |
| 119 | 119 |
| 120 log.Fields{ | 120 log.Fields{ |
| 121 "table": o.LogTable, | 121 "table": o.LogTable, |
| 122 "family": "log", | 122 "family": "log", |
| 123 }.Infof(ctx, "Successfully created 'log' column family.") | 123 }.Infof(ctx, "Successfully created 'log' column family.") |
| 124 } | 124 } |
| 125 | 125 |
| 126 cfg := storage.Config{ | 126 cfg := storage.Config{ |
| 127 MaxLogAge: DefaultMaxLogAge, | 127 MaxLogAge: DefaultMaxLogAge, |
| 128 } | 128 } |
| 129 if err := st.Config(cfg); err != nil { | 129 if err := st.Config(cfg); err != nil { |
| 130 log.WithError(err).Errorf(ctx, "Failed to push default configura
tion.") | 130 log.WithError(err).Errorf(ctx, "Failed to push default configura
tion.") |
| 131 return err | 131 return err |
| 132 } | 132 } |
| 133 | 133 |
| 134 return nil | 134 return nil |
| 135 } | 135 } |
| OLD | NEW |