-
Notifications
You must be signed in to change notification settings - Fork 1
/
atom.xml
535 lines (504 loc) · 49.6 KB
/
atom.xml
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Essentials of JIRA Hacking]]></title>
<link href="http://mou.github.com/atom.xml" rel="self"/>
<link href="http://mou.github.com/"/>
<updated>2011-11-27T06:33:26+04:00</updated>
<id>http://mou.github.com/</id>
<author>
<name><![CDATA[Andrey «MOU» Larionov]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[MemoryPropertySet Bottleneck]]></title>
<link href="http://mou.github.com/blog/2011/10/30/memorypropertyset-bottleneck/"/>
<updated>2011-10-30T02:19:00+04:00</updated>
<id>http://mou.github.com/blog/2011/10/30/memorypropertyset-bottleneck</id>
<content type="html"><![CDATA[<h2>Let’s The Show begin</h2>
<p>With this post I want to start a series of publications covering how you can increase the speed of work for <a href="http://www.atlassian.com/software/jira/overview">Atlassian JIRA</a>. I will consider only the versions starting with 4.0. Unfortunately 4.3 passed by me, so some of the techniques described it might not give the desired effect or simply do not work.</p>
<!--more-->
<h2>About bugs and legacy code</h2>
<p>There is one question which preoccupies the minds of many, one way or another connected with the operation of JIRA. What should be done, what would it work faster? The issue is complex and has more than a few hundred answers. Most often you can hear “Take modern hardware, increase the amount of memory in two times!”. However, what if you have a cpu with 24 cores, 96Gb memory, SSD drives in a RAID array, and the speed of the JIRA still not satisfactory?</p>
<p>There is a paradox, according to which there is no complicated software, which is the JIRA, without errors. Errors can lead as deviant behaviour of the system, as well as to the reduction of performance, if they are associated with multi-threaded code execution. Sufficiently well known problem of <a href="http://en.wikipedia.org/wiki/Legacy_code">legacy code</a>, which was written many years ago, with the use of old technologies, and absolutely other requirements to speed. It is many years later, this code can be the cause of developer’s headaches. But its main problem is that everyone is afraid to change something in it, since it is not always easy to assess the impact of these changes on the system as a whole.</p>
<h2>The Issue</h2>
<p>In this post I would like to consider one of the examples of the legacy code in JIRA. I found this problem when doing profiling and load testing of JIRA for 4.1. Of course I <a href="https://jira.atlassian.com/browse/JRA-21463">reported it</a> in for Atlassian, but the problem is still not resolved. This problem affects to a greater extent versions <strong>4.0</strong>, <strong>4.1</strong> and <strong>4.2</strong>. With JIRA 4.3 I was not confronted, and in 4.4 significantly reduced the influence of this code, but it still exists and is used to manage user preferences.</p>
<p>The essence of the problem: class <code>MemoryPropertySet</code> created to store the settings in memory, and not go after them all the time in the database. However, this class contains the <code>synchronized</code> methods like to read, and to write. And instead of that would increase performance, it is a cause of serious problems in processing a large number of requests. Here is a piece of code which is the cause of cases of major problems:</p>
<figure class='code'><figcaption><span>Striped down source of OpenSymphony MemoryPropertySet</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">synchronized</span> <span class="n">Collection</span> <span class="nf">getKeys</span><span class="o">(</span><span class="n">String</span> <span class="n">prefix</span><span class="o">,</span> <span class="kt">int</span> <span class="n">type</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="o">...</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">public</span> <span class="kd">synchronized</span> <span class="kt">int</span> <span class="nf">getType</span><span class="o">(</span><span class="n">String</span> <span class="n">key</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="o">...</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">public</span> <span class="kd">synchronized</span> <span class="kt">boolean</span> <span class="nf">exists</span><span class="o">(</span><span class="n">String</span> <span class="n">key</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="o">...</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">public</span> <span class="kd">synchronized</span> <span class="kt">void</span> <span class="nf">remove</span><span class="o">(</span><span class="n">String</span> <span class="n">key</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="o">...</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">protected</span> <span class="kd">synchronized</span> <span class="kt">void</span> <span class="nf">setImpl</span><span class="o">(</span><span class="kt">int</span> <span class="n">type</span><span class="o">,</span> <span class="n">String</span> <span class="n">key</span><span class="o">,</span> <span class="n">Object</span> <span class="n">value</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">DuplicatePropertyKeyException</span> <span class="o">{</span>
</span><span class='line'> <span class="o">...</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">protected</span> <span class="kd">synchronized</span> <span class="n">Object</span> <span class="nf">get</span><span class="o">(</span><span class="kt">int</span> <span class="n">type</span><span class="o">,</span> <span class="n">String</span> <span class="n">key</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">InvalidPropertyTypeException</span> <span class="o">{</span>
</span><span class='line'> <span class="o">...</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>As you can see all the methods are <code>synchronized</code> on <code>MemoryPropertySet</code> instance.</p>
<h2>Solution</h2>
<p>To get rid of these problems we need to run JIRA under <strong>Java 5 (1.5)</strong>, but it is better <strong>Java 6 (1.6)</strong>. We did a little change to the original class, that would have been less of a problem. We will replace <code>synchronized</code> units, the use of <code>ReadWriteLock</code> that will not block the reading, if you are not currently writing. Below the code of the modified class:</p>
<figure class='code'><figcaption><span> (MemoryPropertySet.java)</span> <a href='http://mou.github.com/downloads/code/memorypropertyset-bottleneck/MemoryPropertySet.java'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
<span class='line-number'>84</span>
<span class='line-number'>85</span>
<span class='line-number'>86</span>
<span class='line-number'>87</span>
<span class='line-number'>88</span>
<span class='line-number'>89</span>
<span class='line-number'>90</span>
<span class='line-number'>91</span>
<span class='line-number'>92</span>
<span class='line-number'>93</span>
<span class='line-number'>94</span>
<span class='line-number'>95</span>
<span class='line-number'>96</span>
<span class='line-number'>97</span>
<span class='line-number'>98</span>
<span class='line-number'>99</span>
<span class='line-number'>100</span>
<span class='line-number'>101</span>
<span class='line-number'>102</span>
<span class='line-number'>103</span>
<span class='line-number'>104</span>
<span class='line-number'>105</span>
<span class='line-number'>106</span>
<span class='line-number'>107</span>
<span class='line-number'>108</span>
<span class='line-number'>109</span>
<span class='line-number'>110</span>
<span class='line-number'>111</span>
<span class='line-number'>112</span>
<span class='line-number'>113</span>
<span class='line-number'>114</span>
<span class='line-number'>115</span>
<span class='line-number'>116</span>
<span class='line-number'>117</span>
<span class='line-number'>118</span>
<span class='line-number'>119</span>
<span class='line-number'>120</span>
<span class='line-number'>121</span>
<span class='line-number'>122</span>
<span class='line-number'>123</span>
<span class='line-number'>124</span>
<span class='line-number'>125</span>
<span class='line-number'>126</span>
<span class='line-number'>127</span>
<span class='line-number'>128</span>
<span class='line-number'>129</span>
<span class='line-number'>130</span>
<span class='line-number'>131</span>
<span class='line-number'>132</span>
<span class='line-number'>133</span>
<span class='line-number'>134</span>
<span class='line-number'>135</span>
<span class='line-number'>136</span>
<span class='line-number'>137</span>
<span class='line-number'>138</span>
<span class='line-number'>139</span>
<span class='line-number'>140</span>
<span class='line-number'>141</span>
<span class='line-number'>142</span>
<span class='line-number'>143</span>
<span class='line-number'>144</span>
<span class='line-number'>145</span>
<span class='line-number'>146</span>
<span class='line-number'>147</span>
<span class='line-number'>148</span>
<span class='line-number'>149</span>
<span class='line-number'>150</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">package</span> <span class="n">st</span><span class="o">.</span><span class="na">mou</span><span class="o">.</span><span class="na">opensymphony</span><span class="o">.</span><span class="na">module</span><span class="o">.</span><span class="na">propertyset</span><span class="o">.</span><span class="na">memory</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.io.Serializable</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.Collection</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.Collections</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.HashMap</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.Iterator</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.LinkedList</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.List</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.Map</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.concurrent.locks.Lock</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.concurrent.locks.ReadWriteLock</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.concurrent.locks.ReentrantReadWriteLock</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kn">import</span> <span class="nn">com.opensymphony.module.propertyset.AbstractPropertySet</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">com.opensymphony.module.propertyset.DuplicatePropertyKeyException</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">com.opensymphony.module.propertyset.InvalidPropertyTypeException</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MemoryPropertySet</span> <span class="kd">extends</span> <span class="n">AbstractPropertySet</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">private</span> <span class="n">HashMap</span> <span class="n">map</span><span class="o">;</span>
</span><span class='line'> <span class="kd">private</span> <span class="n">ReadWriteLock</span> <span class="n">readWriteLock</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ReentrantReadWriteLock</span><span class="o">();</span>
</span><span class='line'> <span class="kd">private</span> <span class="n">Lock</span> <span class="n">readLock</span> <span class="o">=</span> <span class="n">readWriteLock</span><span class="o">.</span><span class="na">readLock</span><span class="o">();</span>
</span><span class='line'> <span class="kd">private</span> <span class="n">Lock</span> <span class="n">writeLock</span> <span class="o">=</span> <span class="n">readWriteLock</span><span class="o">.</span><span class="na">writeLock</span><span class="o">();</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="n">Collection</span> <span class="nf">getKeys</span><span class="o">(</span><span class="n">String</span> <span class="n">prefix</span><span class="o">,</span> <span class="kt">int</span> <span class="n">type</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">List</span> <span class="n">result</span> <span class="o">=</span> <span class="k">new</span> <span class="n">LinkedList</span><span class="o">();</span>
</span><span class='line'> <span class="n">readLock</span><span class="o">.</span><span class="na">lock</span><span class="o">();</span>
</span><span class='line'> <span class="k">try</span> <span class="o">{</span>
</span><span class='line'> <span class="n">Iterator</span> <span class="n">keys</span> <span class="o">=</span> <span class="n">getMap</span><span class="o">().</span><span class="na">keySet</span><span class="o">().</span><span class="na">iterator</span><span class="o">();</span>
</span><span class='line'>
</span><span class='line'> <span class="k">while</span> <span class="o">(</span><span class="n">keys</span><span class="o">.</span><span class="na">hasNext</span><span class="o">())</span> <span class="o">{</span>
</span><span class='line'> <span class="n">String</span> <span class="n">key</span> <span class="o">=</span> <span class="o">(</span><span class="n">String</span><span class="o">)</span> <span class="n">keys</span><span class="o">.</span><span class="na">next</span><span class="o">();</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">((</span><span class="n">prefix</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">||</span> <span class="n">key</span><span class="o">.</span><span class="na">startsWith</span><span class="o">(</span><span class="n">prefix</span><span class="o">))</span> <span class="o">{</span>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">type</span> <span class="o">==</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">result</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
</span><span class='line'> <span class="n">ValueEntry</span> <span class="n">v</span> <span class="o">=</span> <span class="o">(</span><span class="n">ValueEntry</span><span class="o">)</span> <span class="n">getMap</span><span class="o">().</span><span class="na">get</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">v</span><span class="o">.</span><span class="na">type</span> <span class="o">==</span> <span class="n">type</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">result</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">Collections</span><span class="o">.</span><span class="na">sort</span><span class="o">(</span><span class="n">result</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="n">result</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
</span><span class='line'> <span class="n">readLock</span><span class="o">.</span><span class="na">unlock</span><span class="o">();</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kt">int</span> <span class="nf">getType</span><span class="o">(</span><span class="n">String</span> <span class="n">key</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">readLock</span><span class="o">.</span><span class="na">lock</span><span class="o">();</span>
</span><span class='line'> <span class="k">try</span> <span class="o">{</span>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">getMap</span><span class="o">().</span><span class="na">containsKey</span><span class="o">(</span><span class="n">key</span><span class="o">))</span> <span class="o">{</span>
</span><span class='line'> <span class="k">return</span> <span class="o">((</span><span class="n">ValueEntry</span><span class="o">)</span> <span class="n">getMap</span><span class="o">().</span><span class="na">get</span><span class="o">(</span><span class="n">key</span><span class="o">)).</span><span class="na">type</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
</span><span class='line'> <span class="k">return</span> <span class="mi">0</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
</span><span class='line'> <span class="n">readLock</span><span class="o">.</span><span class="na">unlock</span><span class="o">();</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">exists</span><span class="o">(</span><span class="n">String</span> <span class="n">key</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">return</span> <span class="nf">getType</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="o">></span> <span class="mi">0</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">init</span><span class="o">(</span><span class="n">Map</span> <span class="n">config</span><span class="o">,</span> <span class="n">Map</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">map</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">();</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">remove</span><span class="o">(</span><span class="n">String</span> <span class="n">key</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">writeLock</span><span class="o">.</span><span class="na">lock</span><span class="o">();</span>
</span><span class='line'> <span class="k">try</span> <span class="o">{</span>
</span><span class='line'> <span class="n">getMap</span><span class="o">().</span><span class="na">remove</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
</span><span class='line'> <span class="n">writeLock</span><span class="o">.</span><span class="na">unlock</span><span class="o">();</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">setImpl</span><span class="o">(</span><span class="kt">int</span> <span class="n">type</span><span class="o">,</span> <span class="n">String</span> <span class="n">key</span><span class="o">,</span> <span class="n">Object</span> <span class="n">value</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">DuplicatePropertyKeyException</span> <span class="o">{</span>
</span><span class='line'> <span class="n">writeLock</span><span class="o">.</span><span class="na">lock</span><span class="o">();</span>
</span><span class='line'> <span class="k">try</span> <span class="o">{</span>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">exists</span><span class="o">(</span><span class="n">key</span><span class="o">))</span> <span class="o">{</span>
</span><span class='line'> <span class="n">ValueEntry</span> <span class="n">v</span> <span class="o">=</span> <span class="o">(</span><span class="n">ValueEntry</span><span class="o">)</span> <span class="n">getMap</span><span class="o">().</span><span class="na">get</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">v</span><span class="o">.</span><span class="na">type</span> <span class="o">!=</span> <span class="n">type</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">throw</span> <span class="k">new</span> <span class="nf">DuplicatePropertyKeyException</span><span class="o">();</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">v</span><span class="o">.</span><span class="na">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
</span><span class='line'> <span class="n">getMap</span><span class="o">().</span><span class="na">put</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="k">new</span> <span class="n">ValueEntry</span><span class="o">(</span><span class="n">type</span><span class="o">,</span> <span class="n">value</span><span class="o">));</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
</span><span class='line'> <span class="n">writeLock</span><span class="o">.</span><span class="na">unlock</span><span class="o">();</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">protected</span> <span class="n">HashMap</span> <span class="nf">getMap</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="k">return</span> <span class="n">map</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">protected</span> <span class="n">Object</span> <span class="nf">get</span><span class="o">(</span><span class="kt">int</span> <span class="n">type</span><span class="o">,</span> <span class="n">String</span> <span class="n">key</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">InvalidPropertyTypeException</span> <span class="o">{</span>
</span><span class='line'> <span class="n">readLock</span><span class="o">.</span><span class="na">lock</span><span class="o">();</span>
</span><span class='line'> <span class="k">try</span> <span class="o">{</span>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">exists</span><span class="o">(</span><span class="n">key</span><span class="o">))</span> <span class="o">{</span>
</span><span class='line'> <span class="n">ValueEntry</span> <span class="n">v</span> <span class="o">=</span> <span class="o">(</span><span class="n">ValueEntry</span><span class="o">)</span> <span class="n">getMap</span><span class="o">().</span><span class="na">get</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">v</span><span class="o">.</span><span class="na">type</span> <span class="o">!=</span> <span class="n">type</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">throw</span> <span class="k">new</span> <span class="nf">InvalidPropertyTypeException</span><span class="o">();</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="n">v</span><span class="o">.</span><span class="na">value</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
</span><span class='line'> <span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
</span><span class='line'> <span class="n">readLock</span><span class="o">.</span><span class="na">unlock</span><span class="o">();</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kd">class</span> <span class="nc">ValueEntry</span> <span class="kd">implements</span> <span class="n">Serializable</span> <span class="o">{</span>
</span><span class='line'> <span class="n">Object</span> <span class="n">value</span><span class="o">;</span>
</span><span class='line'> <span class="kt">int</span> <span class="n">type</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="nf">ValueEntry</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="nf">ValueEntry</span><span class="o">(</span><span class="kt">int</span> <span class="n">type</span><span class="o">,</span> <span class="n">Object</span> <span class="n">value</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">this</span><span class="o">.</span><span class="na">type</span> <span class="o">=</span> <span class="n">type</span><span class="o">;</span>
</span><span class='line'> <span class="k">this</span><span class="o">.</span><span class="na">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setType</span><span class="o">(</span><span class="kt">int</span> <span class="n">type</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">this</span><span class="o">.</span><span class="na">type</span> <span class="o">=</span> <span class="n">type</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setValue</span><span class="o">(</span><span class="n">Object</span> <span class="n">value</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">this</span><span class="o">.</span><span class="na">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>There is another class with a similar problem - <code>SerializablePropertySet</code>. He is the direct descendant of the <code>MemoryPropertySet</code> and also contains synchronized method setImpl:</p>
<figure class='code'><figcaption><span> (SerializablePropertySet.java)</span> <a href='http://mou.github.com/downloads/code/memorypropertyset-bottleneck/SerializablePropertySet.java'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">package</span> <span class="n">st</span><span class="o">.</span><span class="na">mou</span><span class="o">.</span><span class="na">opensymphony</span><span class="o">.</span><span class="na">module</span><span class="o">.</span><span class="na">propertyset</span><span class="o">.</span><span class="na">memory</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.io.Serializable</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.HashMap</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">java.util.Map</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kn">import</span> <span class="nn">com.opensymphony.module.propertyset.DuplicatePropertyKeyException</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">com.opensymphony.module.propertyset.IllegalPropertyException</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">SerializablePropertySet</span> <span class="kd">extends</span> <span class="n">MemoryPropertySet</span> <span class="kd">implements</span> <span class="n">Serializable</span> <span class="o">{</span>
</span><span class='line'> <span class="kd">private</span> <span class="n">HashMap</span> <span class="n">serialMap</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">init</span><span class="o">(</span><span class="n">Map</span> <span class="n">config</span><span class="o">,</span> <span class="n">Map</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">serialMap</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">();</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">setImpl</span><span class="o">(</span><span class="kt">int</span> <span class="n">type</span><span class="o">,</span> <span class="n">String</span> <span class="n">key</span><span class="o">,</span> <span class="n">Object</span> <span class="n">value</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">IllegalPropertyException</span><span class="o">,</span> <span class="n">DuplicatePropertyKeyException</span> <span class="o">{</span>
</span><span class='line'> <span class="k">if</span> <span class="o">((</span><span class="n">value</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">&&</span> <span class="o">!(</span><span class="n">value</span> <span class="k">instanceof</span> <span class="n">Serializable</span><span class="o">))</span> <span class="o">{</span>
</span><span class='line'> <span class="k">throw</span> <span class="k">new</span> <span class="nf">IllegalPropertyException</span><span class="o">(</span><span class="s">"Cannot set "</span> <span class="o">+</span> <span class="n">key</span> <span class="o">+</span> <span class="s">". Value type "</span> <span class="o">+</span> <span class="n">value</span><span class="o">.</span><span class="na">getClass</span><span class="o">()</span> <span class="o">+</span> <span class="s">" not Serializable"</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">super</span><span class="o">.</span><span class="na">setImpl</span><span class="o">(</span><span class="n">type</span><span class="o">,</span> <span class="n">key</span><span class="o">,</span> <span class="n">value</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">protected</span> <span class="n">HashMap</span> <span class="nf">getMap</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="k">return</span> <span class="n">serialMap</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>That would compile these classes you need to take a <code>propertyset-1.x.jar</code> from your distribution JIRA. Well, that would make it work will need to change the file <code>atlassian-jira/WEB-INF/classes/propertyset.xml</code>:</p>
<figure class='code'><figcaption><span> (propertyset.xml)</span> <a href='http://mou.github.com/downloads/code/memorypropertyset-bottleneck/propertyset.xml'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="nt"><propertysets></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"aggregate"</span> <span class="na">class=</span><span class="s">"com.opensymphony.module.propertyset.aggregate.AggregatePropertySet"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"cached"</span> <span class="na">class=</span><span class="s">"com.atlassian.jira.propertyset.JiraCachingPropertySet"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"jdbc"</span> <span class="na">class=</span><span class="s">"com.opensymphony.module.propertyset.database.JDBCPropertySet"</span><span class="nt">></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"datasource"</span> <span class="na">value=</span><span class="s">"jdbc/DefaultDS"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"table.name"</span> <span class="na">value=</span><span class="s">"OS_PROPERTYENTRY"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"col.globalKey"</span> <span class="na">value=</span><span class="s">"GLOBAL_KEY"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"col.itemKey"</span> <span class="na">value=</span><span class="s">"ITEM_KEY"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"col.itemType"</span> <span class="na">value=</span><span class="s">"ITEM_TYPE"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"col.string"</span> <span class="na">value=</span><span class="s">"STRING_VALUE"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"col.date"</span> <span class="na">value=</span><span class="s">"DATE_VALUE"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"col.data"</span> <span class="na">value=</span><span class="s">"DATA_VALUE"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"col.float"</span> <span class="na">value=</span><span class="s">"FLOAT_VALUE"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"col.number"</span> <span class="na">value=</span><span class="s">"NUMBER_VALUE"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"></propertyset></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"ejb"</span> <span class="na">class=</span><span class="s">"com.opensymphony.module.propertyset.ejb.EJBPropertySet"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"javabeans"</span> <span class="na">class=</span><span class="s">"com.opensymphony.module.propertyset.javabeans.BeanIntrospectorPropertySet"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"map"</span> <span class="na">class=</span><span class="s">"com.opensymphony.module.propertyset.map.MapPropertySet"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"memory"</span> <span class="na">class=</span><span class="s">"st.mou.opensymphony.module.propertyset.memory.MemoryPropertySet"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"serializable"</span> <span class="na">class=</span><span class="s">"st.mou.opensymphony.module.propertyset.memory.SerializablePropertySet"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"ofbiz"</span> <span class="na">class=</span><span class="s">"com.opensymphony.module.propertyset.ofbiz.OFBizPropertySet"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"hibernate"</span> <span class="na">class=</span><span class="s">"com.opensymphony.module.propertyset.hibernate.HibernatePropertySet"</span><span class="nt">></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"hibernate.connection.datasource"</span> <span class="na">value=</span><span class="s">"jdbs/DefautDS"</span> <span class="nt">/></span>
</span><span class='line'> <span class="nt"><arg</span> <span class="na">name=</span><span class="s">"hibernate.dialect"</span> <span class="na">value=</span><span class="s">"net.sf.hibernate.dialect.HSQLDialect"</span> <span class="nt">/></span>
</span><span class='line'> <span class="nt"></propertyset></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"ojb"</span> <span class="na">class=</span><span class="s">"com.opensymphony.module.propertyset.ojb.OJBPropertySet"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"xml"</span> <span class="na">class=</span><span class="s">"com.opensymphony.module.propertyset.xml.XMLPropertySet"</span><span class="nt">/></span>
</span><span class='line'> <span class="nt"><propertyset</span> <span class="na">name=</span><span class="s">"crowd"</span> <span class="na">class=</span><span class="s">"com.atlassian.crowd.integration.osuser.CrowdPropertySet"</span><span class="nt">/></span>
</span><span class='line'><span class="nt"></propertysets></span>
</span></code></pre></td></tr></table></div></figure>
<p>Look carefully at <strong>19</strong> and <strong>20</strong> lines. That’s the way we change the configuration of the JIRA not to rewrite dozens of calls in the Atalassian code.</p>
<h2>Conclusion</h2>
<p>As we can see, the legacy code needs to be revised and often refactoring few lines of code, can seriously speed up the software.</p>
<p>In the next post I will talk about how to accelerate 4.0, 4.1 without modifying any code.</p>
]]></content>
</entry>
</feed>