Example usage for java.util HashMap replace

List of usage examples for java.util HashMap replace

Introduction

In this page you can find the example usage for java.util HashMap replace.

Prototype

@Override
    public boolean replace(K key, V oldValue, V newValue) 

Source Link

Usage

From source file:com.pearson.eidetic.driver.threads.MonitorSnapshotVolumeTime.java

@Override
public void run() {
    Calendar calendar_int = Calendar.getInstance();

    //0-365/*from  w w  w. j  a v a2 s . c o m*/
    today_ = calendar_int.get(Calendar.DAY_OF_YEAR);

    ConcurrentHashMap<Region, ArrayList<Volume>> localVolumeTime;

    localVolumeTime = awsAccount_.getVolumeTime_Copy();

    for (Map.Entry<Region, ArrayList<Volume>> entry : localVolumeTime.entrySet()) {
        Region region = entry.getKey();
        splitFactorDay_.put(region, 10);
        HashSet<Date> newHashSet = new HashSet<>();
        didMySnapshotDay_.put(entry.getKey(), newHashSet);
    }

    addAlreadyDoneTodaySnapshots(localVolumeTime);

    while (true) {
        try {
            //Reset my stuff
            if (isItTomorrow(today_)) {
                calendar_int = Calendar.getInstance();

                today_ = calendar_int.get(Calendar.DAY_OF_YEAR);
                resetDidMySnapshotDay();

            }

            localVolumeTime = awsAccount_.getVolumeTime_Copy();
            for (Map.Entry<Region, ArrayList<Volume>> entry : localVolumeTime.entrySet()) {
                Region region = entry.getKey();

                if (localVolumeTime.get(region).isEmpty()) {
                    continue;
                }

                timeDay_.put(region, extractRunAt(localVolumeTime.get(region)));

            }

            for (Map.Entry<Region, ArrayList<Volume>> entry : localVolumeTime.entrySet()) {
                Region region = entry.getKey();

                if (localVolumeTime.get(region).isEmpty()) {
                    continue;
                }

                timeDay_.get(region).keySet().removeAll(didMySnapshotDay_.get(region));
                Calendar calendar = Calendar.getInstance();
                Date now = calendar.getTime();
                now = dayFormat_.parse(dayFormat_.format(now));

                List<Date> lessThanNow = findLessThanNow(timeDay_.get(region).keySet(), now);

                if (!lessThanNow.isEmpty()) {
                    for (Date date : lessThanNow) {
                        ArrayList<Volume> volumes = timeDay_.get(region).get(date);
                        List<List<Volume>> listOfLists = Lists.partition(volumes, splitFactorDay_.get(region));

                        if (localVolumeTimeListDay_.get(region) == null
                                || localVolumeTimeListDay_.get(region).isEmpty()) {
                            localVolumeTimeListDay_.put(region, listsToArrayLists(listOfLists));
                        } else {
                            try {
                                localVolumeTimeListDay_.get(region).add(listsToArrayLists(listOfLists).get(0));
                            } catch (Exception e) {
                            }
                        }

                        ArrayList<SnapshotVolumeTime> threads = new ArrayList<>();

                        for (ArrayList<Volume> vols : listsToArrayLists(listOfLists)) {
                            threads.add(new SnapshotVolumeTime(awsAccount_.getAwsAccessKeyId(),
                                    awsAccount_.getAwsSecretKey(), awsAccount_.getUniqueAwsAccountIdentifier(),
                                    awsAccount_.getMaxApiRequestsPerSecond(),
                                    ApplicationConfiguration.getAwsCallRetryAttempts(), region, vols));

                        }

                        didMySnapshotDay_.get(region).add(date);

                        EideticSubThreads_.put(region, threads);

                    }

                }
            }
            //localVolumeTimeListDay now has hashmaps of regions with keys of arrays of arrays of volumes to take snapshots of.

            HashMap<Region, Integer> secsSlept = new HashMap<>();
            HashMap<Region, Boolean> allDead = new HashMap<>();

            for (Map.Entry<Region, ArrayList<Volume>> entry : localVolumeTime.entrySet()) {
                Region region = entry.getKey();

                if (localVolumeTimeListDay_.get(region) == null
                        || localVolumeTimeListDay_.get(region).isEmpty()) {
                    continue;
                }

                //Initializing content
                secsSlept.put(region, 0);

                //Initializing content
                allDead.put(region, false);

                Threads.threadExecutorFixedPool(EideticSubThreads_.get(region), splitFactorDay_.get(region),
                        300, TimeUnit.MINUTES);
            }

            //LETS SEE IF THEY'RE DEAD
            Boolean ejection = false;
            Boolean theyreDead;
            while (true) {
                for (Map.Entry<Region, ArrayList<SnapshotVolumeTime>> entry : EideticSubThreads_.entrySet()) {
                    Region region = entry.getKey();

                    if (areAllThreadsDead(EideticSubThreads_.get(region))) {
                        allDead.put(region, true);
                    } else {
                        secsSlept.replace(region, secsSlept.get(region), secsSlept.get(region) + 1);
                        if (secsSlept.get(region) > 1800) {
                            splitFactorDay_.replace(region, splitFactorDay_.get(region),
                                    splitFactorDay_.get(region) + 1);
                            logger.info(
                                    "Event=\"increasing_splitFactor\", Monitor=\"SnapshotVolumeTime\", splitFactor=\""
                                            + Integer.toString(splitFactorDay_.get(region))
                                            + "\", VolumeTimeSize=\""
                                            + Integer.toString(localVolumeTime.get(region).size()) + "\"");
                            ejection = true;
                            break;
                        }

                    }

                }

                //I dont like this
                theyreDead = true;
                for (Map.Entry<Region, ArrayList<SnapshotVolumeTime>> entry : EideticSubThreads_.entrySet()) {
                    Region region = entry.getKey();

                    //If any of them have false
                    if (!allDead.get(region)) {
                        theyreDead = false;
                    }
                }

                if (ejection || theyreDead) {
                    break;
                }

                Threads.sleepSeconds(1);
            }

            //See if decrease splitfactor
            for (Map.Entry<Region, ArrayList<SnapshotVolumeTime>> entry : EideticSubThreads_.entrySet()) {
                Region region = entry.getKey();

                int timeRemaining = 1800 - secsSlept.get(region);

                if ((splitFactorDay_.get(region) > 5) & (timeRemaining > 60)) {
                    splitFactorDay_.replace(region, splitFactorDay_.get(region),
                            splitFactorDay_.get(region) - 1);
                    logger.info("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                            + "\",Event=\"decreasing_splitFactor\", Monitor=\"SnapshotVolumeNoTime\", splitFactor=\""
                            + Integer.toString(splitFactorDay_.get(region)) + "\", VolumeNoTimeSize=\""
                            + Integer.toString(localVolumeTime.get(region).size()) + "\"");
                }
            }

            localVolumeTimeListDay_.clear();
            EideticSubThreads_.clear();

            Threads.sleepSeconds(30);

        } catch (Exception e) {
            logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                    + "\",Error=\"MonitorSnapshotVolumeTimeFailure\", stacktrace=\"" + e.toString()
                    + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\"");
        }
    }

}