@@ -8,6 +8,7 @@ use proxy_server::{
8
8
proxy:: { gen_proxy_config, gen_tikv_config} ,
9
9
setup:: overwrite_config_with_cmd_args,
10
10
} ;
11
+ use tikv:: config:: MEMORY_USAGE_LIMIT_RATE ;
11
12
use tikv_util:: sys:: SysQuota ;
12
13
13
14
use crate :: proxy:: * ;
@@ -122,6 +123,11 @@ fn test_config_proxy_default_no_config_item() {
122
123
std:: cmp:: min( 256 , ( cpu_num * 8.0 ) as usize )
123
124
) ;
124
125
assert_eq ! ( config. server. status_thread_pool_size, 2 ) ;
126
+
127
+ assert_eq ! ( config. raft_store. evict_cache_on_memory_ratio, 0.1 ) ;
128
+ assert_eq ! ( config. memory_usage_high_water, 0.9 ) ;
129
+ // Seems #244 doesn't goes into this branch.
130
+ assert_eq ! ( config. server. reject_messages_on_memory_ratio, 0.2 ) ;
125
131
}
126
132
127
133
/// We test if the engine-label is set properly.
@@ -200,3 +206,149 @@ apply-low-priority-pool-size = 41
200
206
config. raft_store. apply_batch_system. low_priority_pool_size
201
207
) ;
202
208
}
209
+
210
+ #[ test]
211
+ fn test_memory_limit_overwrite ( ) {
212
+ let app = App :: new ( "RaftStore Proxy" )
213
+ . arg (
214
+ Arg :: with_name ( "memory-limit-size" )
215
+ . long ( "memory-limit-size" )
216
+ . help ( "Used as the maximum memory we can consume, in bytes" )
217
+ . takes_value ( true ) ,
218
+ )
219
+ . arg (
220
+ Arg :: with_name ( "memory-limit-ratio" )
221
+ . long ( "memory-limit-ratio" )
222
+ . help ( "Used as the maximum memory we can consume, in percentage" )
223
+ . takes_value ( true ) ,
224
+ ) ;
225
+
226
+ let bootstrap = |args : Vec < & str > | {
227
+ let mut v: Vec < String > = vec ! [ ] ;
228
+ let matches = app. clone ( ) . get_matches_from ( args) ;
229
+ let mut config = gen_tikv_config ( & None , false , & mut v) ;
230
+ let mut proxy_config = gen_proxy_config ( & None , false , & mut v) ;
231
+ proxy_config. raftdb . defaultcf . block_cache_size = ReadableSize ( 0 ) ;
232
+ proxy_config. rocksdb . defaultcf . block_cache_size = ReadableSize ( 0 ) ;
233
+ proxy_config. rocksdb . lockcf . block_cache_size = ReadableSize ( 0 ) ;
234
+ proxy_config. rocksdb . writecf . block_cache_size = ReadableSize ( 0 ) ;
235
+ overwrite_config_with_cmd_args ( & mut config, & mut proxy_config, & matches) ;
236
+ address_proxy_config ( & mut config, & proxy_config) ;
237
+ config. compatible_adjust ( ) ;
238
+ config
239
+ } ;
240
+
241
+ {
242
+ let args = vec ! [
243
+ "test_memory_limit_overwrite1" ,
244
+ "--memory-limit-size" ,
245
+ "12345" ,
246
+ ] ;
247
+ let mut config = bootstrap ( args) ;
248
+ assert ! ( config. validate( ) . is_ok( ) ) ;
249
+ assert_eq ! ( config. memory_usage_limit, Some ( ReadableSize ( 12345 ) ) ) ;
250
+ }
251
+
252
+ {
253
+ let args = vec ! [
254
+ "test_memory_limit_overwrite2" ,
255
+ "--memory-limit-size" ,
256
+ "12345" ,
257
+ "--memory-limit-ratio" ,
258
+ "0.9" ,
259
+ ] ;
260
+ let mut config = bootstrap ( args) ;
261
+ assert ! ( config. validate( ) . is_ok( ) ) ;
262
+ assert_eq ! ( config. memory_usage_limit, Some ( ReadableSize ( 12345 ) ) ) ;
263
+ }
264
+
265
+ let total = SysQuota :: memory_limit_in_bytes ( ) ;
266
+ {
267
+ let args = vec ! [
268
+ "test_memory_limit_overwrite3" ,
269
+ "--memory-limit-ratio" ,
270
+ "0.800000" ,
271
+ ] ;
272
+ let mut config = bootstrap ( args) ;
273
+ assert ! ( config. validate( ) . is_ok( ) ) ;
274
+ let limit = ( total as f64 * 0.8 ) as u64 ;
275
+ assert_eq ! ( config. memory_usage_limit, Some ( ReadableSize ( limit) ) ) ;
276
+ }
277
+
278
+ let default_limit = ( total as f64 * MEMORY_USAGE_LIMIT_RATE ) as u64 ;
279
+ {
280
+ let args = vec ! [
281
+ "test_memory_limit_overwrite4" ,
282
+ "--memory-limit-ratio" ,
283
+ "7.9" ,
284
+ ] ;
285
+ let mut config = bootstrap ( args) ;
286
+ assert ! ( config. validate( ) . is_ok( ) ) ;
287
+ assert_eq ! ( config. memory_usage_limit, Some ( ReadableSize ( default_limit) ) ) ;
288
+ }
289
+
290
+ {
291
+ let args = vec ! [
292
+ "test_memory_limit_overwrite5" ,
293
+ "--memory-limit-ratio" ,
294
+ "'-0.9'" ,
295
+ ] ;
296
+ let mut config = bootstrap ( args) ;
297
+ assert ! ( config. validate( ) . is_ok( ) ) ;
298
+ assert_eq ! ( config. memory_usage_limit, Some ( ReadableSize ( default_limit) ) ) ;
299
+ }
300
+
301
+ {
302
+ let args = vec ! [ "test_memory_limit_overwrite6" ] ;
303
+ let mut config = bootstrap ( args) ;
304
+ assert ! ( config. validate( ) . is_ok( ) ) ;
305
+ assert_eq ! ( config. memory_usage_limit, Some ( ReadableSize ( default_limit) ) ) ;
306
+ }
307
+
308
+ let bootstrap2 = |args : Vec < & str > | {
309
+ let mut v: Vec < String > = vec ! [ ] ;
310
+ let matches = app. clone ( ) . get_matches_from ( args) ;
311
+ let mut file = tempfile:: NamedTempFile :: new ( ) . unwrap ( ) ;
312
+ write ! (
313
+ file,
314
+ "
315
+ memory-usage-limit = 42
316
+ "
317
+ )
318
+ . unwrap ( ) ;
319
+ let path = file. path ( ) ;
320
+ let cpath = Some ( path. as_os_str ( ) ) ;
321
+ let mut config = gen_tikv_config ( & cpath, false , & mut v) ;
322
+ let mut proxy_config = gen_proxy_config ( & cpath, false , & mut v) ;
323
+ proxy_config. raftdb . defaultcf . block_cache_size = ReadableSize ( 0 ) ;
324
+ proxy_config. rocksdb . defaultcf . block_cache_size = ReadableSize ( 0 ) ;
325
+ proxy_config. rocksdb . lockcf . block_cache_size = ReadableSize ( 0 ) ;
326
+ proxy_config. rocksdb . writecf . block_cache_size = ReadableSize ( 0 ) ;
327
+ overwrite_config_with_cmd_args ( & mut config, & mut proxy_config, & matches) ;
328
+ address_proxy_config ( & mut config, & proxy_config) ;
329
+ config. compatible_adjust ( ) ;
330
+ config
331
+ } ;
332
+
333
+ {
334
+ let args = vec ! [
335
+ "test_memory_limit_nooverwrite3" ,
336
+ "--memory-limit-ratio" ,
337
+ "0.800000" ,
338
+ ] ;
339
+ let mut config = bootstrap2 ( args) ;
340
+ assert ! ( config. validate( ) . is_ok( ) ) ;
341
+ assert_eq ! ( config. memory_usage_limit, Some ( ReadableSize ( 42 ) ) ) ;
342
+ }
343
+
344
+ {
345
+ let args = vec ! [
346
+ "test_memory_limit_nooverwrite1" ,
347
+ "--memory-limit-size" ,
348
+ "12345" ,
349
+ ] ;
350
+ let mut config = bootstrap2 ( args) ;
351
+ assert ! ( config. validate( ) . is_ok( ) ) ;
352
+ assert_eq ! ( config. memory_usage_limit, Some ( ReadableSize ( 42 ) ) ) ;
353
+ }
354
+ }
0 commit comments