Subversion Repositories SmartDukaan

Rev

Rev 37448 | Rev 37472 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30308 amit.gupta 1
package com.smartdukaan.cron.scheduled;
2
 
3
import com.spice.profitmandi.dao.model.ImeiActivationTimestampModel;
4
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
30353 amit.gupta 5
import org.apache.logging.log4j.LogManager;
6
import org.apache.logging.log4j.Logger;
30308 amit.gupta 7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.stereotype.Component;
9
 
37447 amit 10
import java.util.ArrayList;
30308 amit.gupta 11
import java.util.List;
12
import java.util.stream.Collectors;
13
 
14
@Component
15
public class StandAlone {
16
 
17
	@Autowired
30352 amit.gupta 18
	private OppoImeiActivationService oppoImeiActivationService;
30308 amit.gupta 19
 
20
	@Autowired
34418 amit.gupta 21
	private RealmeImeiActivationService realmeImeiActivationService;
22
 
23
	@Autowired
37459 amit 24
	private MotorolaImeiActivationService motorolaImeiActivationService;
25
 
26
	@Autowired
30352 amit.gupta 27
	private ActivatedImeiRepository activatedImeiRepository;
30308 amit.gupta 28
 
30353 amit.gupta 29
	private static final Logger LOGGER = LogManager.getLogger(StandAlone.class);
30
 
30308 amit.gupta 31
	public void checkOppoImeiStatus() throws Exception {
37448 amit 32
		List<ImeiActivationTimestampModel> imeiActivationTimestampModels = activatedImeiRepository.selectImeiActivationPendingByBrand("Oppo", 1, 25);
30308 amit.gupta 33
		List<String> imeis = imeiActivationTimestampModels.stream().map(x -> x.getSerialNumber()).collect(Collectors.toList());
36253 amit 34
		LOGGER.info("Oppo secondary imeis {}", imeis);
30352 amit.gupta 35
		oppoImeiActivationService.updateActivationDate(imeis);
30308 amit.gupta 36
	}
34418 amit.gupta 37
 
36253 amit 38
	public void checkOppoImeiStatusTertiary() throws Exception {
37448 amit 39
		List<ImeiActivationTimestampModel> imeiActivationTimestampModels = activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Oppo", 1, 25);
36253 amit 40
		List<String> imeis = imeiActivationTimestampModels.stream().map(x -> x.getSerialNumber()).collect(Collectors.toList());
41
		LOGGER.info("Oppo tertiary imeis {}", imeis);
42
		oppoImeiActivationService.updateActivationDate(imeis);
43
	}
44
 
34418 amit.gupta 45
	public void checkRealmeImeiStatus() throws Exception {
37448 amit 46
		List<ImeiActivationTimestampModel> imeiActivationTimestampModels = activatedImeiRepository.selectImeiActivationPendingByRealme("Realme", 1, 12);
34418 amit.gupta 47
		List<String> imeis = imeiActivationTimestampModels.stream().map(x -> x.getSerialNumber()).collect(Collectors.toList());
36253 amit 48
		LOGGER.info("Realme secondary imeis {}", imeis);
34418 amit.gupta 49
		realmeImeiActivationService.updateActivationDate(imeis);
50
	}
36253 amit 51
 
52
	public void checkRealmeImeiStatusTertiary() throws Exception {
37448 amit 53
		List<ImeiActivationTimestampModel> imeiActivationTimestampModels = activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Realme", 1, 12);
36253 amit 54
		List<String> imeis = imeiActivationTimestampModels.stream().map(x -> x.getSerialNumber()).collect(Collectors.toList());
55
		LOGGER.info("Realme tertiary imeis {}", imeis);
56
		realmeImeiActivationService.updateActivationDate(imeis);
57
	}
37447 amit 58
 
59
	/**
60
	 * Secondary + tertiary in ONE browser session per brand.
61
	 *
62
	 * They were separate @Scheduled jobs, so each brand span up two ChromeDrivers at
63
	 * once and the fleet ran four. Each driver tree costs roughly 850MB, and this box
64
	 * co-hosts a 9.4GB tomcat with ~3GB available -- it has been OOM-killed twice this
65
	 * month and tomcat was the victim both times. Halving concurrent drivers is the
66
	 * single biggest memory saving available here.
67
	 *
68
	 * Nothing else changes: the two pools differ only in which named query fills them,
69
	 * and both already funnel into the same updateActivationDate -> checkWarranty ->
70
	 * saveActivation path. The pools are disjoint by construction (secondary excludes
71
	 * anything with a FofoLineItem, tertiary is FofoLineItem-based); distinct() is cheap
72
	 * insurance rather than a fix for a known overlap.
73
	 *
74
	 *
75
	 * Sizing: merging SERIALISES work that used to run in parallel, so keeping the old
76
	 * batch sizes would cost throughput. Measured post-cap at 10.2s/imei (oppo) and
77
	 * 14.2s/imei (realme), solving M * 86400 / (300 + M*t):
78
	 *   oppo    2 parallel jobs x10 = 4,299/day  ->  merged 30 = 4,277/day  (parity)
79
	 *   realme  2 parallel jobs x10 = 3,910/day  ->  merged 20 = 2,959/day  (-24%)
80
	 * Oppo is sized to hold parity because it is already short of its 4,798/day need.
81
	 * Realme is left at 20 -- it needs 2,243/day, so it can absorb the dip in exchange
82
	 * for shorter batches.
83
	 *
84
	 * Note what this does and does not save: total driver-SECONDS are unchanged (that is
85
	 * the point of resizing), but PEAK concurrent drivers halves from 4 to 2. Peak is
86
	 * what triggers the OOM killer, not average.
87
	 *
88
	 * The per-pool log lines are kept so existing log analysis still works.
89
	 */
90
	public void checkOppoImeiStatusCombined() throws Exception {
37448 amit 91
		List<String> secondary = activatedImeiRepository.selectImeiActivationPendingByBrand("Oppo", 1, 15)
37447 amit 92
				.stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
37448 amit 93
		List<String> tertiary = activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Oppo", 1, 15)
37447 amit 94
				.stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
95
		LOGGER.info("Oppo secondary imeis {}", secondary);
96
		LOGGER.info("Oppo tertiary imeis {}", tertiary);
97
		List<String> all = new ArrayList<>(secondary);
98
		all.addAll(tertiary);
99
		all = all.stream().distinct().collect(Collectors.toList());
100
		if (all.isEmpty()) {
101
			LOGGER.info("Oppo: nothing pending, not starting a browser");
102
			return;
103
		}
104
		oppoImeiActivationService.updateActivationDate(all);
105
	}
106
 
107
	public void checkRealmeImeiStatusCombined() throws Exception {
37448 amit 108
		List<String> secondary = activatedImeiRepository.selectImeiActivationPendingByRealme("Realme", 1, 12)
37447 amit 109
				.stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
37448 amit 110
		List<String> tertiary = activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Realme", 1, 12)
37447 amit 111
				.stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
112
		LOGGER.info("Realme secondary imeis {}", secondary);
113
		LOGGER.info("Realme tertiary imeis {}", tertiary);
114
		List<String> all = new ArrayList<>(secondary);
115
		all.addAll(tertiary);
116
		all = all.stream().distinct().collect(Collectors.toList());
117
		if (all.isEmpty()) {
118
			LOGGER.info("Realme: nothing pending, not starting a browser");
119
			return;
120
		}
121
		realmeImeiActivationService.updateActivationDate(all);
122
	}
123
 
37459 amit 124
	/**
125
	 * Motorola: secondary + tertiary in ONE browser session, same shape as the
126
	 * oppo/realme combined jobs.
127
	 *
128
	 * Cadence: the pool query defers an imei for `days` after each attempt
129
	 * (saveActivation bumps createTimestamp even when no date came back), so
130
	 * days=2 gives the requested "retry everything every two days".
131
	 *
132
	 * Sizing: the pending pool measured 1,534 (1,182 secondary + 352 tertiary).
133
	 * At the ~10-14s/imei the oppo and realme jobs measure, 60 per invocation is
134
	 * roughly 12 minutes of driver time, and clearing 1,534 inside 48h needs
135
	 * about 26 invocations -- i.e. an OS cron entry every 90 minutes, with
136
	 * headroom. Do NOT schedule it inside the oppo/realme window: each driver
137
	 * tree costs ~850MB and this box has been OOM-killed twice with tomcat the
138
	 * victim, so peak concurrent drivers is the number that matters.
139
	 */
140
	public void checkMotorolaImeiStatusCombined() throws Exception {
141
		List<String> secondary = activatedImeiRepository.selectImeiActivationPendingByBrand("Motorola", 2, 30)
142
				.stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
143
		List<String> tertiary = activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Motorola", 2, 30)
144
				.stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
145
		LOGGER.info("Motorola secondary imeis {}", secondary);
146
		LOGGER.info("Motorola tertiary imeis {}", tertiary);
147
		List<String> all = new ArrayList<>(secondary);
148
		all.addAll(tertiary);
149
		all = all.stream().distinct().collect(Collectors.toList());
150
		if (all.isEmpty()) {
151
			LOGGER.info("Motorola: nothing pending, not starting a browser");
152
			return;
153
		}
154
		motorolaImeiActivationService.updateActivationDate(all);
155
	}
156
 
36253 amit 157
}