-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
options.go
144 lines (123 loc) · 4.33 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 runtime
import (
"flag"
"sync"
)
// GlobalOptions are the global options for the active graph. Options can be
// defined at any time before execution and are re-created by the harness on
// remote execution workers. Global options should be used sparingly.
var GlobalOptions = NewOptions()
// NewOptions provides an initialized set of options. It
// is only intended for framework and test use.
func NewOptions() *Options {
return &Options{opt: make(map[string]string)}
}
// Options are untyped options.
type Options struct {
opt map[string]string
ro bool
mu sync.Mutex
}
// RawOptions represents exported options as JSON-serializable data.
// Exact representation is subject to change.
type RawOptions struct {
Options map[string]string `json:"options"`
}
// TODO(herohde) 7/7/2017: Dataflow has a concept of sdk pipeline options and
// various values in this map:
//
// { "display_data": [...],
// "options":{
// "autoscalingAlgorithm":"NONE",
// "dataflowJobId":"2017-07-07_xxx",
// "gcpTempLocation":"",
// "maxNumWorkers":0,
// "numWorkers":1,
// "project":"xxx",
// "options": <Go SDK pipeline options>,
// }}
//
// Which we may or may not want to be visible as first-class pipeline options.
// It is also TBD how/if to support global display data, but we certainly don't
// want it served back to the harness.
// TODO(herohde) 3/12/2018: remove the extra options wrapper and the bogus
// fields current required by the Java runners.
// RawOptionsWrapper wraps RawOptions to the form expected by the
// harness. The extra layer is currently needed due to Dataflow
// expectations about this representation. Subject to change.
type RawOptionsWrapper struct {
Options RawOptions `json:"beam:option:go_options:v1"`
Runner string `json:"beam:option:runner:v1"`
AppName string `json:"beam:option:app_name:v1"`
Experiments []string `json:"beam:option:experiments:v1"`
RetainDocker bool `json:"beam:option:retain_docker_containers:v1"`
Parallelism int `json:"beam:option:parallelism:v1"`
}
// Import imports the options from previously exported data and makes the
// options read-only. It panics if import is called twice.
func (o *Options) Import(opt RawOptions) {
o.mu.Lock()
defer o.mu.Unlock()
if o.ro {
panic("import failed: options read-only")
}
o.ro = true
o.opt = copyMap(opt.Options)
}
// Get returns the value of the key. If the key has not been set, it returns "".
func (o *Options) Get(key string) string {
o.mu.Lock()
defer o.mu.Unlock()
return o.opt[key]
}
// Set defines the value of the given key. If the key is already defined, it
// panics.
func (o *Options) Set(key, value string) {
o.mu.Lock()
defer o.mu.Unlock()
if o.ro {
return // ignore silently to allow init-time set of options
}
o.opt[key] = value
}
// Export returns a JSON-serializable copy of the options.
func (o *Options) Export() RawOptions {
o.mu.Lock()
defer o.mu.Unlock()
return RawOptions{Options: copyMap(o.opt)}
}
// LoadOptionsFromFlags adds any flags not defined in excludeFlags to the options.
// If the key is already defined, it ignores that flag.
func (o *Options) LoadOptionsFromFlags(excludeFlags map[string]bool) {
o.mu.Lock()
defer o.mu.Unlock()
if o.ro {
return // ignore silently to allow init-time set of options
}
flag.Visit(func(f *flag.Flag) {
if !excludeFlags[f.Name] && o.opt[f.Name] == "" {
o.opt[f.Name] = f.Value.String()
}
})
}
func copyMap(m map[string]string) map[string]string {
ret := make(map[string]string)
for k, v := range m {
ret[k] = v
}
return ret
}