-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLecture 12 Distributed Transactions.srt
5042 lines (4197 loc) · 142 KB
/
Lecture 12 Distributed Transactions.srt
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1
00:00:00,540 --> 00:00:15,509
好的,今天的话题是分布式交易,这些交易确实是为了
all right today's topic is distributed
transactions and these come in really to
2
00:00:15,509 --> 00:00:20,010
实施部分,这就是我将介绍它们的第一部分
implementation pieces and that's how
I'll cover them the first big piece of
3
00:00:20,010 --> 00:00:35,100
并发控制第二个是原子提交,也是分布式的原因
concurrency control the second is atomic
commit and the reason why distributive
4
00:00:35,100 --> 00:00:38,640
交易的出现是,这对于有大量交易的人来说非常常见
transactions come up is that it's very
frequent for people with large amounts
5
00:00:38,640 --> 00:00:43,980
数据最终在许多不同的服务器上拆分或分片数据,因此
of data to end up splitting or sharding
the data over many different servers so
6
00:00:43,980 --> 00:00:49,320
也许如果您经营一家银行,例如,您一半的银行余额
maybe if you're running a bank for
example the bank balances for half your
7
00:00:49,320 --> 00:00:53,040
客户是一台服务器,另一半是银行余额
customers are one server and the bank
balances for the other half are on a
8
00:00:53,040 --> 00:00:56,610
不同的服务器让我们做到这一点,例如将负载分为处理负载和
different server let's do it like split
the load both the processing load and
9
00:00:56,610 --> 00:01:02,190
还有其他方面的空间需求,也许您正在录制
the space requirements this comes up for
other things too maybe you're recording
10
00:01:02,190 --> 00:01:07,380
您知道某个网站上的文章的票数很重要
vote counts on articles at a website you
know the maybe there's so many millions
11
00:01:07,380 --> 00:01:11,580
数百万票数的一半是,并且在一台服务器上,
millions of articles half the vote
counts are and are on one server and
12
00:01:11,580 --> 00:01:18,510
一半或一半以上的癌症,但有些手术需要触摸修改或
half the vote cancer or another but some
operations require touching modifying or
13
00:01:18,510 --> 00:01:21,659
在多个不同的服务器上读取数据,所以如果我们在做银行
reading data on multiple different
servers so if we're doing a bank
14
00:01:21,659 --> 00:01:25,440
从一个客户转移到另一口井,他们的余额可能不同
transfer from one customer into another
well their balances may be on different
15
00:01:25,440 --> 00:01:29,490
服务器,因此为了达到平衡,我们必须修改数据读取和
servers and therefore in order to do the
balance we have to modify data read and
16
00:01:29,490 --> 00:01:37,290
在两台不同的服务器上写入数据,我们真的很想以一种方式构建
write data on two different servers and
we'd really like to or one way building
17
00:01:37,290 --> 00:01:40,619
这些系统,我们将在稍后的课程中以其他方式构建其他系统
these systems and we'll see others later
on in the course one way to build the
18
00:01:40,619 --> 00:01:46,020
系统只是试图隐藏将数据拆分为多个数据的复杂性
system just try to hide the complexity
of splitting this data across multiple
19
00:01:46,020 --> 00:01:51,810
服务器尝试向应用程序程序员隐藏它,这就像
servers try to hide it from the
application programmer and this is like
20
00:01:51,810 --> 00:01:56,939
数十年来,传统上一直是数据库问题,因此
traditionally has been a database
concern for for many decades and so a
21
00:01:56,939 --> 00:02:00,840
当今的许多材料都起源于数据库,但是这些思想已被使用
lot of today's material originated with
databases but the ideas have been used
22
00:02:00,840 --> 00:02:04,650
在分布式系统中更为广泛,您不必将其称为
much more widely in distributed systems
which you wouldn't necessarily call a
23
00:02:04,650 --> 00:02:13,019
人们通常打包并发的传统数据库
traditional database the way people sort
of usually package up concurrency
24
00:02:13,019 --> 00:02:15,739
控制加
control plus
25
00:02:16,620 --> 00:02:30,959
原子提交在抽象上称为交易,我们之前已经看到过
atomic commit is in abstraction called a
transaction which we've seen before and
26
00:02:30,959 --> 00:02:36,519
这个想法是,您认识的程序员有很多不同的操作可能
the idea is that the programmer you know
has a bunch of different operations may
27
00:02:36,519 --> 00:02:40,690
在数据库中的不同记录上,他们希望所有这些操作
be on different records in the database
they'd like all those operations to be
28
00:02:40,690 --> 00:02:45,730
单个单元的排序,不会因故障或其他观察而拆分
sort of a single unit and not split by
failures or by observation from other
29
00:02:45,730 --> 00:02:52,120
活动和交易处理系统将需要
activities and the transaction
processing system will require the
30
00:02:52,120 --> 00:02:56,260
程序员标记阅读顺序的开始和结束,
programmer to mark the beginning and the
end of that sequence of reading and
31
00:02:56,260 --> 00:02:59,739
编写和更新操作,以标记开始和结束
writing and updating operations in order
to mark the beginning and of the
32
00:02:59,739 --> 00:03:03,099
交易和交易处理系统肯定会
transaction and the transaction
processing system has certainly will
33
00:03:03,099 --> 00:03:07,090
提供有关开始和结束之间发生的某些保证
provide certain guarantees about what
happens between the beginning and the
34
00:03:07,090 --> 00:03:11,200
结束,例如,假设我们正在运行
end
so for example supposing we're running
35
00:03:11,200 --> 00:03:19,299
我们的银行,并且我们希望从用户X的帐户转帐到的帐户
our bank and we want to do a transfer
from account of user X to the account of
36
00:03:19,299 --> 00:03:23,910
现在,整个用户的余额都从10开始,因此最初
user wide now these balances from both
of them start out as 10 so initially
37
00:03:23,910 --> 00:03:35,560
暴露10 y等于10和x和y我的意思是成为数据库中的记录,我们
expose 10 y equals 10 and x and y I'm
mean to be records in a database and we
38
00:03:35,560 --> 00:03:40,239
要转移,我们实际上会想象有两笔交易
want to transfer we will actually
imagine that there's two transactions
39
00:03:40,239 --> 00:03:44,829
可能同时运行一个从帐户X转移美元的操作
that might be running at the same time
one to transfer a dollar from account X
40
00:03:44,829 --> 00:03:49,989
到帐户Y和另一笔交易对所有帐户进行审计
to account Y and the other transaction
to do an audit of of all the accounts at
41
00:03:49,989 --> 00:03:53,410
银行确保银行中的总金额永远不会
the bank to make sure that the total
amount of money in the bank never
42
00:03:53,410 --> 00:03:56,769
会发生变化,因为毕竟,如果您进行转账,您应该知道总数不应
changes because after all if you do
transfers you know the total shouldn't
43
00:03:56,769 --> 00:04:01,180
即使您在多个帐户之间转移资金也可以更改
change even if you move money between
accounts in order to express this with
44
00:04:01,180 --> 00:04:07,090
交易我们可能有两个交易,第一个交易呼叫
transactions we might have two
transactions the first transaction call
45
00:04:07,090 --> 00:04:12,850
t1是程序员应该标记的传输标记。
it t1 is the transfer well mark the
programmer is expected to mark the
46
00:04:12,850 --> 00:04:17,978
以begin交易开始
beginning of it with the begin
transaction which all right at the
47
00:04:17,978 --> 00:04:23,919
开始,然后对两个记录中两个记录的两个余额进行操作
beginning and then the operations on the
two balances on the two records in the
48
00:04:23,919 --> 00:04:27,130
数据库,所以我们可以添加[音乐]
database so we might add
[Music]
49
00:04:27,130 --> 00:04:42,669
一个可能将余额X加一个,然后将Y加-1,然后我们需要标记结尾
one might add one the balance X and add
-1 to Y and then we need to mark the end
50
00:04:42,669 --> 00:04:49,220
根据交易当前,我们可能有一笔要检查的交易
by the transaction currently we might
have a transaction that's going to check
51
00:04:49,220 --> 00:04:52,820
所有余额对所有余额进行审计,以查找总金额或查看所有余额
all the balance do an audit of all the
balances find the sum or look at all the
52
00:04:52,820 --> 00:04:56,600
余额确保它们加起来即使不改变也不会改变
balances make sure they add up to the
number that doesn't change despite
53
00:04:56,600 --> 00:05:04,570
转账,所以我在考虑审计交易的第二笔交易
transfers so the second transaction I'm
thinking about the audit transaction
54
00:05:05,590 --> 00:05:13,430
我们还需要标记起点和终点,这一次我们只是在阅读
also we need to mark the beginning and
end this time we're just reading there's
55
00:05:13,430 --> 00:05:19,550
只读交易,我们需要获取所有帐户的当前余额
a read-only transaction we need to get
the current balances of all the accounts
56
00:05:19,550 --> 00:05:24,260
列出他们现在只是这两个帐户,所以我们有两个临时帐户
lists they were just these two accounts
for now so we have two temporary
57
00:05:24,260 --> 00:05:32,660
我们将要读取的第一个变量将是余额X的值
variables we're gonna read the first one
it's going to be the value of balance X
58
00:05:32,660 --> 00:05:40,729
恰到好处意味着我们正在读取该记录,我们也读取Y,然后打印
just right get to mean we're reading
that record we also read Y and we print
59
00:05:40,729 --> 00:05:55,700
他们俩,这是交易的结束,问题是什么是
them both and that's the end of the
transaction the question is what are
60
00:05:55,700 --> 00:05:59,660
这两项交易的法律结果是我们要做的第一件事
legal results from these two
transactions that's the first thing we
61
00:05:59,660 --> 00:06:03,620
要确定的是给定的起始状态就是两个
want to establish is what are you know
given the starting state namely the two
62
00:06:03,620 --> 00:06:07,669
十美元的余额以及您跑步后的最终结果是什么
balances for ten dollars and what could
be the final results after you've run
63
00:06:07,669 --> 00:06:12,050
这些交易可能同时进行,所以我们需要一个概念
both these transactions maybe at the
same time so we need a notion of what
64
00:06:12,050 --> 00:06:17,330
是正确的,一旦我们知道我们需要能够制造机械
would be correct and once we know that
we need to be able to build machinery
65
00:06:17,330 --> 00:06:23,060
实际上将能够执行这些事务并仅获得那些
that will actually be able to execute
these transactions and get only those
66
00:06:23,060 --> 00:06:28,190
尽管存在并发和失败,但仍能给出正确答案,所以首先是正确性
correct answers despite concurrency and
failures so first what's correctness
67
00:06:28,190 --> 00:06:36,580
良好的数据库通常具有正确性的概念,称为酸
well databases usually have a notion of
correctness called acid
68
00:06:38,110 --> 00:06:48,770
或bb-8是酸,它代表原子,这意味着
or bb-8 is acid and it stands for atomic
and this means that a transaction that
69
00:06:48,770 --> 00:06:50,960
有多个步骤,您可能会写多个不同的步骤
has multiple steps
you know maybe writes multiple different
70
00:06:50,960 --> 00:06:55,940
记录尽管有故障但还是有故障,应该所有权利
records if there's a failure despite
failures either all of the right should
71
00:06:55,940 --> 00:07:00,080
完成或全部都不做,不应该因为尴尬而失败
be done or none of them it shouldn't be
the case that a failure at an awkward
72
00:07:00,080 --> 00:07:04,340
事务中间的时间应该使更新完成的一半
time in the middle of a transaction
should leave half the updates completed
73
00:07:04,340 --> 00:07:16,160
看不见,一半的更新从未完成,要么全部要么一无所有,所以这是或
invisible and half the updates never
done it's all or nothing so this is or
74
00:07:16,160 --> 00:07:32,000
尽管有失败,但C代表一致,实际上我们并没有
not despite failures the C stands for
consistent it's actually we're not going
75
00:07:32,000 --> 00:07:38,030
担心这一点通常是指数据库将
to worry about that that's usually meant
to refer to the fact that database will
76
00:07:38,030 --> 00:07:43,610
强制应用程序声明的某些不变式,这不是我们真正的
enforce certain invariants declared by
the application it's not really our
77
00:07:43,610 --> 00:07:49,389
尽管今天很重要,但通常代表孤立
concern today the I though it's quite
important it usually stands for isolated
78
00:07:49,389 --> 00:07:54,830
这实际上是两个事务是否在运行的属性
and this is a really a property of
whether or not two transactions that run
79
00:07:54,830 --> 00:07:58,610
同时可以在交易发生之前看到彼此的变化
at the same time can see each other's
changes before the transactions have
80
00:07:58,610 --> 00:08:02,630
完成了他们是否可以看到某种中间更新以及
finished whether or not they can see
sort of intermediate updates and from
81
00:08:02,630 --> 00:08:11,360
在另一笔交易的中间,您的目标是不,而
the middle of another transaction and
your goal is no and the sort of
82
00:08:11,360 --> 00:08:17,539
大多数人通常所说的隔离技术特有的东西是
technical specific thing that most
people generally mean by isolation is
83
00:08:17,539 --> 00:08:21,139
事务执行是可序列化的,我将说明
that the transaction execution is
serializable and I'll explain what that
84
00:08:21,139 --> 00:08:29,630
意味着有点,但是归结为交易看不到对方的
means in a bit but it boils down to
transactions can't see each other's
85
00:08:29,630 --> 00:08:34,909
更改看不到中间状态,而只能看到完整的交易结果
changes can't see intermediate states
but only complete transaction results
86
00:08:34,909 --> 00:08:38,830
最后的D代表耐用
and the final D stands for durable
87
00:08:39,969 --> 00:08:44,800
这意味着交易在客户或其他任何对象之后提交
and this means that after a transaction
commits after the client or whatever
88
00:08:44,800 --> 00:08:49,000
提交交易的程序会从数据库获得回复
program that submitted the transaction
gets a reply back from the database
89
00:08:49,000 --> 00:08:52,769
说是的,您知道我们已经执行了您的交易
saying yes
you know we've executed your transaction
90
00:08:52,769 --> 00:08:58,689
酸中的D表示交易会修改数据库
the D in acid means that the
transactions modifications the database
91
00:08:58,689 --> 00:09:02,620
会很持久,因为它们仍会存在,不会被某些物体擦除
will be durable that they'll still be
there they won't be erased by a some
92
00:09:02,620 --> 00:09:08,199
某种故障,实际上这意味着必须将内容写入
sort of failure and in practice that
means that stuff has to be written into
93
00:09:08,199 --> 00:09:13,540
一些非易失性存储持久性存储(例如磁盘),所以今天您是
some non-volatile storage persistent
storage like a disk and so today you are
94
00:09:13,540 --> 00:09:18,660
实际上,在整个课程中,我们的关注点都将围绕着
in fact for this whole course really our
concerns are going to revolve around
95
00:09:18,660 --> 00:09:25,509
关于失败的良好行为良好的尊重
good behavior with respect to failure
good respect good behavior with respect
96
00:09:25,509 --> 00:09:31,120
多个并行活动中的其他活动,并确保数据
to other from multiple parallel
activities and making sure that the data
97
00:09:31,120 --> 00:09:40,660
即使有东西崩溃,他们仍然在那里吗?
is there still they are after even if
something crashes so the most
98
00:09:40,660 --> 00:09:44,949
对我们来说,有趣的部分是孤立冰的具体定义
interesting part of this for us is the
specific definition of ice of isolated
99
00:09:44,949 --> 00:09:51,459
或可序列化,所以在讨论它之前,我将对其进行布局
or serializable so I'm going to lay that
out before before talking about how it
100
00:09:51,459 --> 00:10:03,639
实际上适用于这些交易,因此通常将孤立的ioan和
actually applies to these transactions
so the ioan isolated is usually and the
101
00:10:03,639 --> 00:10:10,329
如果执行一组交易,则对此的定义
definition for this if a set of
transactions executes you know
102
00:10:10,329 --> 00:10:16,720
同时或多或少同时是您的结果集和
concurrently more or less at the same
time they you are the set of results and
103
00:10:16,720 --> 00:10:21,879
这里的结果指的是由任何人创建的新数据库记录
here the results refer to both the new
database records created by any
104
00:10:21,879 --> 00:10:26,829
修改交易可能执行的操作,以及
modifications the transactions might do
and in addition any output that the
105
00:10:26,829 --> 00:10:30,970
产生了如此广泛的交易,因为这两个交易增加了
transaction is produced so broader
transactions these two adds since they
106
00:10:30,970 --> 00:10:34,480
变更记录他们的需求变更记录是结果的一部分,并且
change records their needs change
records are part of the results and the
107
00:10:34,480 --> 00:10:38,319
此print语句的输出是结果的一部分,因此
output of this print statement is part
of the results so the definition of
108
00:10:38,319 --> 00:10:44,850
可序列化表示结果可序列化
serializable says the results are
serializable
109
00:10:47,250 --> 00:11:20,610
如果存在一些执行交易的顺序,那么我们要说
if there exists some order of execution
of the transactions so we're gonna say a
110
00:11:20,610 --> 00:11:24,930
事务的特定执行并行并发执行是
specific execution parallel concurrent
execution of transactions is
111
00:11:24,930 --> 00:11:31,430
可序列化,如果存在某个序列顺序,则在此实际上强调了序列
serializable if there exists some serial
order really emphasizing serial here a
112
00:11:31,430 --> 00:11:37,110
产生相同结果的相同事务的执行顺序
serial order of execution of those same
transactions that yields the same result
113
00:11:37,110 --> 00:11:40,139
因为实际执行与此处的差异是实际
as the actual execution and the
difference of here is the actual
114
00:11:40,139 --> 00:11:46,470
执行中可能有很多并行性,但这是必需的
execution may have had a lot of
parallelism in it but it's required to
115
00:11:46,470 --> 00:11:49,470
一次产生与某个结果相同的结果
produce the same result as some one at a
time
116
00:11:49,470 --> 00:11:54,629
执行相同的交易,因此您检查是否
execution of the same transactions and
so the way you check whether an
117
00:11:54,629 --> 00:11:59,040
执行是否可序列化是一些并发执行是否可序列化
execution is serializable whether some
concurrent execution is serializable is
118
00:11:59,040 --> 00:12:03,720
您查看结果,看看是否一次可以找到一个
you look at the results and see if you
can find actually some one at a time
119
00:12:03,720 --> 00:12:09,180
执行确实产生相同结果的相同交易,因此对于我们
execution of the same transactions that
does produce the same results so for our
120
00:12:09,180 --> 00:12:16,139
交易在这里,只有两个订单,一次只有两个
transaction up here there's only two
orders there's only two one at a time
121
00:12:16,139 --> 00:12:22,170
串行订单可用事务1然后事务2或事务2然后
serial orders available transaction 1
then transaction 2 or transaction 2 then
122
00:12:22,170 --> 00:12:27,509
交易1,所以我们只看一下它们将产生的结果
transaction 1 and so we can just look at
the results that they would produce if
123
00:12:27,509 --> 00:12:34,910
在这些命令中的每个命令中一次执行一个,所以如果我们先执行t1然后执行t2
executed one at a time in each of these
orders so if we execute t1 and then t2
124
00:12:34,910 --> 00:12:41,839
那么我们得到x等于11
then we get x equals 11
125
00:12:42,240 --> 00:12:49,750
为什么等于9和此打印语句,因为t1首先执行了此打印
why equals 9 and this print statement
since t1 executed first this print
126
00:12:49,750 --> 00:12:58,690
语句看到这两个更新的值,因此它将打印字符串11 9
statement sees these two updated values
and so it will print the string 11 9 the
127
00:12:58,690 --> 00:13:06,279
其他可能的顺序是,也许先运行t2,然后再运行t1,在这种情况下
other possible order is that perhaps t2
ran first and then t1 and in that case
128
00:13:06,279 --> 00:13:11,980
t2在修改之前会看到2条记录,但是修改后
t2 will see that 2 records before they
were modified but the modifications will
129
00:13:11,980 --> 00:13:16,750
由于t1稍后运行,因此仍会发生,因此最终结果将再次为x等于
still take place since t1 runs later so
the final results will again be x equals
130
00:13:16,750 --> 00:13:27,600
11 y等于9,但是这次t2在我们的值之前浸透了,所以这是两个
11 y equal 9 but this time t2 sodded
before our values so these are the two
131
00:13:27,600 --> 00:13:34,480
可序列化的合法结果,以及如果我们发现运行中还有其他问题
legal results for serializability and if
we ever see anything else from running
132
00:13:34,480 --> 00:13:38,110
这两个事务同时我们将知道数据库是
these two transactions at the same time
we'll know that the database were
133
00:13:38,110 --> 00:13:42,130
运行不提供正在执行的可序列化执行
running against does not provide
serializable execution it's doing
134
00:13:42,130 --> 00:13:48,820
还有其他的事情,所以当我们在思考如果或
something else and so while we're
thinking through what would happen if or
135
00:13:48,820 --> 00:13:52,900
如果始终违背这些AHA将会发生什么,这是仅有的两个
what would happen if will always be
against these AHA these are the only two
136
00:13:52,900 --> 00:13:56,910
合法的结果,我们最好做一些能够产生彼此的事情
legal results we better be doing
something that produces one or the other
137
00:13:56,910 --> 00:14:02,950
有趣的是,有不止一种可能的结果取决于
it's interesting to note that there's
more than one possible result depending
138
00:14:02,950 --> 00:14:06,430
根据实际订单,如果您在
on the actual order you if you you
submit these two transactions at the
139
00:14:06,430 --> 00:14:11,980
同时您不知道是t1 t2还是t2 t1,所以您必须
same time you don't know whether it's
gonna be t1 t2 or t2 t1 so you have to
140
00:14:11,980 --> 00:14:15,310
愿意期待不止一种可能的法律结果,并且如您所愿
be willing to expect more than one
possible legal result and as you have
141
00:14:15,310 --> 00:14:18,459
更多或同时运行的事务更加复杂
more or transactions running
concurrently a more complicated there
142
00:14:18,459 --> 00:14:22,810
可能有很多很多可能不同的正确结果
may be many many possible different
correct results that are all
143
00:14:22,810 --> 00:14:28,600
可序列化,因为这里有很多订单可以用来完成此任务
serializable because of many many orders
here that could be used to fulfill this
144
00:14:28,600 --> 00:14:35,560
需求还可以,所以现在我们有了正确性的定义,甚至
requirement okay so now that we have a
definition of correctness and we even
145
00:14:35,560 --> 00:14:42,400
知道所有可能的结果是什么我们可以问几个问题
know what all the possible results are
we can ask a few questions so few
146
00:14:42,400 --> 00:14:48,610
关于如何执行的假设问题,例如,假设
what-if questions about how these could
execute so for example suppose that the
147
00:14:48,610 --> 00:14:53,260
系统实际执行此操作的方式是它开始了事务2,
way the system actually executed this
was that it started transaction 2 and
148
00:14:53,260 --> 00:14:58,180
读完X之后就达到了
got as far as
just after reading X and then
149
00:14:58,180 --> 00:15:05,350
交易一在这一点上运行,然后在交易一完成后
transaction one ran at this point and
then after transaction one finished
150
00:15:05,350 --> 00:15:13,819
现在继续执行交易,结果与其他交易不同
transaction to continue executing now it
turns out in with different other
151
00:15:13,819 --> 00:15:18,259
比这实际上可能合法的交易,但在这里我们要
transactions than this that might
actually be legal but here we want to
152
00:15:18,259 --> 00:15:22,279
知道这是否合法,所以我们想知道天哪,我们是否真的以这种方式执行
know if it's legal so we're wondering
gosh if we actually executed that way
153
00:15:22,279 --> 00:15:27,350
我们会得到什么结果,它们是否与这两个方法中的任何一个相同,如果
what results will we get and are they
the same as either of these two well if
154
00:15:27,350 --> 00:15:32,509
我们在这里执行事务一,那么t1将看到值10 t2将看到
we execute transaction one here then t1
is gonna see value 10 t2 is gonna see
155
00:15:32,509 --> 00:15:38,749
Y递减后的值,因此t1将为10 t2将为9,这是什么
the value after decrementing Y so t1
will be 10 t2 will be 9 and what this
156
00:15:38,749 --> 00:15:45,439
打印将是10 9,而这两个输出都不是,所以这意味着
print will be 10 9 and that's neither of
these two outputs here so that means
157
00:15:45,439 --> 00:15:49,369
以我刚刚绘制的方式执行,无法序列化,因此不会
executing in this way that I just drew
is not serializable it would not be
158
00:15:49,369 --> 00:15:57,860
法律上另一个有趣的问题是,如果我们开始执行交易该怎么办
legal another interesting question is
what if we started executing transaction
159
00:15:57,860 --> 00:16:02,420
1,我们就到达了第一个广告之后,然后所有
1 and we got as far as just after the
first ad and then at that point all the
160
00:16:02,420 --> 00:16:10,939
事务2在这里执行,因此这意味着X是值
transaction 2 executed right here so
that would mean at this point X is value
161
00:16:10,939 --> 00:16:20,179
11事务2将读取为1110现在打印1110,而1110不是其中之一
11 the transaction 2 would read 1110 now
print 1110 and 1110 is not one of these
162
00:16:20,179 --> 00:16:23,749
两个合法值,因此对于这两个而言,执行也是不合法的
two legal values so this execution is
also not legal for these two
163
00:16:23,749 --> 00:16:26,589
交易
transactions
164
00:16:35,510 --> 00:16:43,110
所以可序列化可序列化之所以流行且有用的原因
so the reason why serializable
serializability is a popular and useful
165
00:16:43,110 --> 00:16:47,280
定义交易正确执行的含义
definition of what it means for
transactions to be correct for execution
166
00:16:47,280 --> 00:16:51,030
正确的交易是对程序员来说这是一个非常简单的模型
of transactions to be correct is that
it's a very easy model for programmers
167
00:16:51,030 --> 00:16:56,190
您可以编写复杂的交易而不必担心其他事情
you can write complicated transactions
without having to worry about what else
168
00:16:56,190 --> 00:16:59,370