xuekang
2024-05-10 edf3b7fde038fcf3e6d86b8b4b88c2ff6f9014cf
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 *  Copyright 1999-2019 Seata.io Group.
 *
 *  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 io.seata.server.store;
 
import io.seata.common.util.StringUtils;
import io.seata.config.Configuration;
import io.seata.config.ConfigurationFactory;
import io.seata.core.constants.ConfigurationKeys;
import io.seata.server.env.ContainerHelper;
import io.seata.server.storage.file.FlushDiskMode;
 
import static io.seata.common.DefaultValues.SERVER_DEFAULT_STORE_MODE;
import static io.seata.core.constants.ConfigurationKeys.STORE_FILE_PREFIX;
 
/**
 * @author lizhao
 */
public class StoreConfig {
 
    private static final Configuration CONFIGURATION = ConfigurationFactory.getInstance();
    private static StoreMode storeMode;
    private static SessionMode sessionMode;
    private static LockMode lockMode;
 
    /**
     * set storeMode sessionMode lockMode from StartupParameter
     *
     * @param storeMode   storeMode
     * @param sessionMode sessionMode
     * @param lockMode    lockMode
     */
    public static void setStartupParameter(String storeMode, String sessionMode, String lockMode) {
        if (StringUtils.isNotBlank(storeMode)) {
            StoreConfig.storeMode = StoreMode.get(storeMode);
        }
        if (StringUtils.isNotBlank(sessionMode)) {
            StoreConfig.sessionMode = SessionMode.get(sessionMode);
        }
        if (StringUtils.isNotBlank(lockMode)) {
            StoreConfig.lockMode = LockMode.get(lockMode);
        }
    }
 
    /**
     * Default 16kb.
     */
    private static final int DEFAULT_MAX_BRANCH_SESSION_SIZE = 1024 * 16;
 
    /**
     * Default 512b.
     */
    private static final int DEFAULT_MAX_GLOBAL_SESSION_SIZE = 512;
 
    /**
     * Default 16kb.
     */
    private static final int DEFAULT_WRITE_BUFFER_SIZE = 1024 * 16;
 
    public static int getMaxBranchSessionSize() {
        return CONFIGURATION.getInt(STORE_FILE_PREFIX + "maxBranchSessionSize", DEFAULT_MAX_BRANCH_SESSION_SIZE);
    }
 
    public static int getMaxGlobalSessionSize() {
        return CONFIGURATION.getInt(STORE_FILE_PREFIX + "maxGlobalSessionSize", DEFAULT_MAX_GLOBAL_SESSION_SIZE);
    }
 
    public static int getFileWriteBufferCacheSize() {
        return CONFIGURATION.getInt(STORE_FILE_PREFIX + "fileWriteBufferCacheSize", DEFAULT_WRITE_BUFFER_SIZE);
    }
 
    public static FlushDiskMode getFlushDiskMode() {
        return FlushDiskMode.findDiskMode(CONFIGURATION.getConfig(STORE_FILE_PREFIX + "flushDiskMode"));
    }
 
    /**
     * only for inner call
     *
     * @return
     */
    private static StoreMode getStoreMode() {
        //startup
        if (null != storeMode) {
            return storeMode;
        }
        //env
        String storeModeEnv = ContainerHelper.getStoreMode();
        if (StringUtils.isNotBlank(storeModeEnv)) {
            return StoreMode.get(storeModeEnv);
        }
        //config
        String storeModeConfig = CONFIGURATION.getConfig(ConfigurationKeys.STORE_MODE, SERVER_DEFAULT_STORE_MODE);
        return StoreMode.get(storeModeConfig);
    }
 
    public static SessionMode getSessionMode() {
        //startup
        if (null != sessionMode) {
            return sessionMode;
        }
        //env
        String sessionModeEnv = ContainerHelper.getSessionStoreMode();
        if (StringUtils.isNotBlank(sessionModeEnv)) {
            return SessionMode.get(sessionModeEnv);
        }
        //config
        String sessionModeConfig = CONFIGURATION.getConfig(ConfigurationKeys.STORE_SESSION_MODE);
        if (StringUtils.isNotBlank(sessionModeConfig)) {
            return SessionMode.get(sessionModeConfig);
        }
        // complication old config
        return SessionMode.get(getStoreMode().name());
    }
 
    public static LockMode getLockMode() {
        //startup
        if (null != lockMode) {
            return lockMode;
        }
        //env
        String lockModeEnv = ContainerHelper.getLockStoreMode();
        if (StringUtils.isNotBlank(lockModeEnv)) {
            return LockMode.get(lockModeEnv);
        }
        //config
        String lockModeConfig = CONFIGURATION.getConfig(ConfigurationKeys.STORE_LOCK_MODE);
        if (StringUtils.isNotBlank(lockModeConfig)) {
            return LockMode.get(lockModeConfig);
        }
        // complication old config
        return LockMode.get(getStoreMode().name());
    }
 
    public enum StoreMode {
        /**
         * The File store mode.
         */
        FILE("file"),
        /**
         * The Db store mode.
         */
        DB("db"),
        /**
         * The Redis store mode.
         */
        REDIS("redis");
 
        private String name;
 
        StoreMode(String name) {
            this.name = name;
        }
 
        public String getName() {
            return name;
        }
 
        public static StoreMode get(String name) {
            for (StoreMode mode : StoreMode.values()) {
                if (mode.getName().equalsIgnoreCase(name)) {
                    return mode;
                }
            }
            throw new IllegalArgumentException("unknown store mode:" + name);
        }
    }
 
    public enum SessionMode {
        /**
         * The File store mode.
         */
        FILE("file"),
        /**
         * The Db store mode.
         */
        DB("db"),
        /**
         * The Redis store mode.
         */
        REDIS("redis");
 
        private String name;
 
        SessionMode(String name) {
            this.name = name;
        }
 
        public String getName() {
            return name;
        }
 
        public static SessionMode get(String name) {
            for (SessionMode mode : SessionMode.values()) {
                if (mode.getName().equalsIgnoreCase(name)) {
                    return mode;
                }
            }
            throw new IllegalArgumentException("unknown session mode:" + name);
        }
    }
 
    public enum LockMode {
        /**
         * The File store mode.
         */
        FILE("file"),
        /**
         * The Db store mode.
         */
        DB("db"),
        /**
         * The Redis store mode.
         */
        REDIS("redis");
 
        private String name;
 
        LockMode(String name) {
            this.name = name;
        }
 
        public String getName() {
            return name;
        }
 
        public static LockMode get(String name) {
            for (LockMode mode : LockMode.values()) {
                if (mode.getName().equalsIgnoreCase(name)) {
                    return mode;
                }
            }
            throw new IllegalArgumentException("unknown lock mode:" + name);
        }
    }
 
}