This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
/
BookingService.java
300 lines (266 loc) · 11.6 KB
/
BookingService.java
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Copyright 2019, Futurewei Technologies
* <p>
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.bluemarlin.ims.imsservice.service;
import com.bluemarlin.ims.imsservice.exceptions.BookingNotFoundException;
import com.bluemarlin.ims.imsservice.exceptions.ESConnectionException;
import com.bluemarlin.ims.imsservice.exceptions.FailedLockingException;
import com.bluemarlin.ims.imsservice.exceptions.NoInventoryExceptions;
import com.bluemarlin.ims.imsservice.dao.booking.BookingDaoESImp;
import com.bluemarlin.ims.imsservice.dao.inventory.InventoryEstimateDaoESImp;
import com.bluemarlin.ims.imsservice.dao.tbr.TBRDao;
import com.bluemarlin.ims.imsservice.model.BookResult;
import com.bluemarlin.ims.imsservice.model.Booking;
import com.bluemarlin.ims.imsservice.model.BookingBucket;
import com.bluemarlin.ims.imsservice.model.Day;
import com.bluemarlin.ims.imsservice.model.InventoryResult;
import com.bluemarlin.ims.imsservice.model.Range;
import com.bluemarlin.ims.imsservice.model.TargetingChannel;
import com.bluemarlin.ims.imsservice.util.CommonUtil;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;
@Service
public class BookingService extends BaseService
{
public static final long HIGH_PRICE = Long.MAX_VALUE;
private static final int MAX_LOCKING_ATTEMPTS = 60;
private static ExecutorService executor = Executors.newCachedThreadPool();
@Autowired
private InventoryEstimateService inventoryEstimateService;
public BookingService(){
}
/**
* This method allocate the requested inventory among the booking-buckets through following steps:
* <p>
* 1. Sort booking buckets
* 2. Ignore irrelevant booking-buckets (the ones with zero cross intersection with targeting channel).
* 3. Calculate the inventory that is outside of booking-bucket, if the amount is less than the amount booked on
* BB then BB is full; otherwise split the BB and allocate the amount on new one.
*
* @param bookingDayMap
* @param booking
* @param targetingChannel
* @param day
* @return
* @throws IOException
* @throws JSONException
* @throws ESConnectionException
*/
private void bookingAllocation(Map<Day, List<Booking>> bookingDayMap,
Booking booking, TargetingChannel targetingChannel, Day day, AtomicLong atomicRequestAmount, AtomicLong atomicBookedAmount) throws IOException, JSONException, ESConnectionException, ExecutionException, InterruptedException
{
List<BookingBucket> removed = Collections.synchronizedList(new ArrayList<>());
List<BookingBucket> added = Collections.synchronizedList(new ArrayList<>());
Set<Day> daySet = new HashSet<>(Arrays.asList(new Day[]{day}));
Map<Day, List<BookingBucket>> bookingBucketMap = this.bookingDao.getBookingBuckets(daySet);
Map<String, Booking> bookingsMapForDay = Booking.buildBookingMap(bookingDayMap.get(day));
if (bookingBucketMap.size() > 0 && bookingDayMap.size() > 0)
{
List<BookingBucket> bookingBuckets = bookingBucketMap.get(day);
List<Future<Long>> bookedOnBookingBucketFutures = new ArrayList<>();
for (BookingBucket bookingBucket : bookingBuckets)
{
if (!inventoryEstimateService.hasIntersection(bookingBucket, targetingChannel, bookingsMapForDay))
{
continue;
}
BookingBucketAllocation bookingAllocationOnBucketTask = new BookingBucketAllocation(inventoryEstimateService, bookingBucket, bookingsMapForDay, booking, targetingChannel
, day, new BookingBucketAllocation.UtilVariables(added, removed, atomicRequestAmount, atomicBookedAmount));
Future futureForBookingBucket = executor.submit(bookingAllocationOnBucketTask);
bookedOnBookingBucketFutures.add(futureForBookingBucket);
}
for (Future<Long> _future : bookedOnBookingBucketFutures)
{
_future.get();
}
}
/**
* Deleting removed BBs and Add new ones.
*/
for (BookingBucket bb : removed)
{
bookingDao.removeBookingBucket(bb);
}
for (BookingBucket bb : added)
{
bookingDao.createBookingBucket(bb);
}
return;
}
private void acquireLock() throws InterruptedException, FailedLockingException
{
int attempts = MAX_LOCKING_ATTEMPTS;
while (!bookingDao.lockBooking())
{
Thread.sleep(1000);
attempts--;
if (attempts < 0)
{
throw new FailedLockingException();
}
}
}
public BookingService(InventoryEstimateService inventoryEstimateService, InventoryEstimateDaoESImp inventoryEstimateDao, TBRDao tbrDao, BookingDaoESImp bookingDao)
{
this.inventoryEstimateService = inventoryEstimateService;
this.inventoryEstimateDao = inventoryEstimateDao;
this.tbrDao = tbrDao;
this.bookingDao = bookingDao;
}
/**
* This service removes a booking.
* The underlying methods find all the booking buckets that are corresponded to bookingId and set their allocation amount to zero.
*
* @param bookingId
* @return
*/
public void removeBooking(String bookingId) throws IOException, JSONException, BookingNotFoundException
{
bookingDao.removeBooking(bookingId);
}
/**
* This service retrieves all the bookings associated to an advertiser id.
*
* @param advId
* @return
* @throws IOException
*/
public List<Booking> getBookingsByAdvId(String advId) throws IOException, ESConnectionException, BookingNotFoundException
{
List<Booking> result = bookingDao.getBookings(advId);
if (CommonUtil.isEmpty(result))
{
throw new BookingNotFoundException();
}
return result;
}
/**
* This service allocate inventory for a specific targeting channel for a specific period of time.
* The service has the following steps:
* <p>
* 1. Look on booking index so that no booking occur concurrently
* 2. Check if enough inventory exists
* 3. Create booking document in booking index
* 4. For each day, create a booking bucket and perform booking allocation
* 5. Unlock booking index
*
* @param targetingChannel
* @param ranges
* @param price
* @param advId
* @param requestCount
* @return
* @throws ESConnectionException
* @throws JSONException
* @throws IOException
* @throws NoInventoryExceptions
* @throws InterruptedException
* @throws FailedLockingException
* @throws ExecutionException
*/
public BookResult book(TargetingChannel targetingChannel, List<Range> ranges, double price, String advId, long requestCount) throws ESConnectionException, JSONException, IOException, NoInventoryExceptions, InterruptedException, FailedLockingException, ExecutionException
{
/**
* Locking booking index before start the operation.
*/
acquireLock();
BookResult bookResult = null;
try
{
InventoryResult inventoryResult = inventoryEstimateService.aggregateInventory(targetingChannel, ranges, price);
if (inventoryResult.getAvailCount() < requestCount)
{
throw new NoInventoryExceptions();
}
/**
* Creating a new booking document.
*/
Booking booking = new Booking(targetingChannel, ranges, price, advId, requestCount);
bookingDao.createBooking(booking);
/**
* Creating a booking bucket for each day, if it is necessary.
*/
List<Day> sortedDays = Day.buildSortedDays(ranges);
Set<Day> daySet = new HashSet<>(sortedDays);
Map<Day, List<Booking>> bookingDayMap = this.bookingDao.getBookings(daySet);
AtomicLong atomicRequestCount = new AtomicLong(requestCount);
AtomicLong atomicBookedAmount = new AtomicLong(0);
List<Future<Long>> futureList = new ArrayList<>();
for (Day day : sortedDays)
{
Future bookedForDayFuture = executor.submit((Callable<Object>) () ->
{
BookingBucket maxPriorityBB = bookingDao.getMaxBookingBucketPriority(day.toString());
BookingBucket bookingBucket;
List<Booking> bookings = bookingDayMap.get(day);
List<String> previousBookingIds = new ArrayList<>();
int priority = 0;
if (maxPriorityBB != null)
{
previousBookingIds = Booking.extractBookingIds(bookings);
previousBookingIds.remove(booking.getBookingId());
priority = maxPriorityBB.getPriority() + 1;
}
bookingBucket = new BookingBucket(day.toString(), booking.getBookingId(), previousBookingIds, priority + 1);
bookingDao.createBookingBucket(bookingBucket);
/**
* Starting allocation process
*/
bookingAllocation(bookingDayMap, booking, targetingChannel, day, atomicRequestCount, atomicBookedAmount);
return 0;
});
futureList.add(bookedForDayFuture);
}
for (Future _future : futureList)
{
_future.get();
}
bookResult = new BookResult(booking.getBookingId(), atomicBookedAmount.get());
}
catch (Exception e)
{
throw e;
}
finally
{
bookingDao.unlockBooking();
}
return bookResult;
}
public Booking getBookingById(String bookingId) throws IOException, ESConnectionException
{
return bookingDao.getBooking(bookingId);
}
}