|
50 | 50 | import java.net.URL;
|
51 | 51 | import java.nio.file.Files;
|
52 | 52 | import java.nio.file.Paths;
|
53 |
| -import java.util.ArrayList; |
54 | 53 | import java.util.Date;
|
| 54 | +import java.util.ArrayList; |
55 | 55 | import java.util.HashSet;
|
56 | 56 | import java.util.LinkedHashSet;
|
57 | 57 | import java.util.List;
|
58 | 58 | import java.util.Map;
|
59 | 59 | import java.util.Set;
|
60 | 60 | import java.util.concurrent.ConcurrentHashMap;
|
61 | 61 | import java.util.concurrent.CopyOnWriteArrayList;
|
| 62 | +import java.util.concurrent.locks.ReentrantLock; |
62 | 63 |
|
63 | 64 | import static com.alipay.sofa.ark.spi.constant.Constants.BIZ_TEMP_WORK_DIR_RECYCLE_FILE_SUFFIX;
|
64 | 65 | import static com.alipay.sofa.ark.spi.constant.Constants.ACTIVATE_MULTI_BIZ_VERSION_ENABLE;
|
@@ -379,31 +380,42 @@ private void doStart(String[] args, Map<String, String> envs) throws Throwable {
|
379 | 380 |
|
380 | 381 | BizManagerService bizManagerService = ArkServiceContainerHolder.getContainer().getService(
|
381 | 382 | BizManagerService.class);
|
| 383 | + ReentrantLock bizLock = bizManagerService.getBizLock(bizName); |
382 | 384 |
|
383 |
| - // case0: active the first module as activated |
384 |
| - if (bizManagerService.getActiveBiz(bizName) == null) { |
385 |
| - setBizState(BizState.ACTIVATED, StateChangeReason.STARTED); |
386 |
| - return; |
387 |
| - } |
| 385 | + // lock the bizName to ensure biz with same name install in sequence |
| 386 | + bizLock.lock(); |
| 387 | + try { |
388 | 388 |
|
389 |
| - // case1: support multiple version biz as activated: always activate the new version and keep the old module activated |
390 |
| - boolean activateMultiBizVersion = Boolean.parseBoolean(ArkConfigs.getStringValue( |
391 |
| - ACTIVATE_MULTI_BIZ_VERSION_ENABLE, "false")); |
392 |
| - if (activateMultiBizVersion) { |
393 |
| - setBizState(BizState.ACTIVATED, StateChangeReason.STARTED); |
394 |
| - return; |
395 |
| - } |
| 389 | + // case0: active the first module as activated |
| 390 | + if (bizManagerService.getActiveBiz(bizName) == null) { |
| 391 | + setBizState(BizState.ACTIVATED, StateChangeReason.STARTED); |
| 392 | + return; |
| 393 | + } |
| 394 | + |
| 395 | + // case1: support multiple version biz as activated: always activate the new version and keep the old module activated |
| 396 | + boolean activateMultiBizVersion = Boolean.parseBoolean(ArkConfigs.getStringValue( |
| 397 | + ACTIVATE_MULTI_BIZ_VERSION_ENABLE, "false")); |
| 398 | + if (activateMultiBizVersion) { |
| 399 | + setBizState(BizState.ACTIVATED, StateChangeReason.STARTED); |
| 400 | + return; |
| 401 | + } |
396 | 402 |
|
397 |
| - // case2: always activate the new version and deactivate the old module according to ACTIVATE_NEW_MODULE config |
398 |
| - if (Boolean.getBoolean(Constants.ACTIVATE_NEW_MODULE)) { |
399 |
| - Biz currentActiveBiz = bizManagerService.getActiveBiz(bizName); |
400 |
| - ((BizModel) currentActiveBiz).setBizState(BizState.DEACTIVATED, |
401 |
| - StateChangeReason.SWITCHED, String.format("switch to new biz %s", getIdentity())); |
402 |
| - setBizState(BizState.ACTIVATED, StateChangeReason.STARTED, |
403 |
| - String.format("switch from old biz: %s", currentActiveBiz.getIdentity())); |
404 |
| - } else { |
405 |
| - // case3: always deactivate the new version and keep old module activated according to ACTIVATE_NEW_MODULE config |
406 |
| - setBizState(BizState.DEACTIVATED, StateChangeReason.STARTED, "start but is deactivated"); |
| 403 | + // case2: always activate the new version and deactivate the old module according to ACTIVATE_NEW_MODULE config |
| 404 | + if (Boolean.getBoolean(Constants.ACTIVATE_NEW_MODULE)) { |
| 405 | + Biz currentActiveBiz = bizManagerService.getActiveBiz(bizName); |
| 406 | + ((BizModel) currentActiveBiz).setBizState(BizState.DEACTIVATED, |
| 407 | + StateChangeReason.SWITCHED, |
| 408 | + String.format("switch to new biz %s", getIdentity())); |
| 409 | + setBizState(BizState.ACTIVATED, StateChangeReason.STARTED, |
| 410 | + String.format("switch from old biz: %s", currentActiveBiz.getIdentity())); |
| 411 | + } else { |
| 412 | + // case3: always deactivate the new version and keep old module activated according to ACTIVATE_NEW_MODULE config |
| 413 | + setBizState(BizState.DEACTIVATED, StateChangeReason.STARTED, |
| 414 | + "start but is deactivated"); |
| 415 | + } |
| 416 | + } finally { |
| 417 | + // ensure the lock will be released, avoid deadlock |
| 418 | + bizLock.unlock(); |
407 | 419 | }
|
408 | 420 | }
|
409 | 421 |
|
|
0 commit comments