├── .gitignore ├── docs ├── perf.png └── snowflake-64bit.jpg ├── src ├── test │ └── java │ │ └── cn │ │ └── izern │ │ ├── sequence │ │ ├── RepeatedTest.java │ │ ├── SequenceTest1.java │ │ ├── ContiPerfTest.java │ │ └── SequenceTest.java │ │ └── hibernate │ │ └── id │ │ └── Demo.java └── main │ └── java │ └── cn │ ├── izern │ ├── hibernate │ │ └── id │ │ │ └── IDSequenceGenerator.java │ └── sequence │ │ └── Sequence.java │ └── ms │ └── sequence │ └── SystemClock.java ├── README.md ├── pom.xml └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .git/ 2 | .project 3 | .classpath 4 | .settings/ 5 | /target/ 6 | -------------------------------------------------------------------------------- /docs/perf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/izern/sequence/HEAD/docs/perf.png -------------------------------------------------------------------------------- /docs/snowflake-64bit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/izern/sequence/HEAD/docs/snowflake-64bit.jpg -------------------------------------------------------------------------------- /src/test/java/cn/izern/sequence/RepeatedTest.java: -------------------------------------------------------------------------------- 1 | package cn.izern.sequence; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import org.junit.Test; 7 | 8 | import cn.izern.sequence.Sequence; 9 | 10 | public class RepeatedTest { 11 | 12 | /** 13 | * 重复性测试 14 | */ 15 | @Test 16 | public void testRepeated() { 17 | Set set = new HashSet(); 18 | int maxTimes = 1000000 * 10; 19 | Sequence sequence = new Sequence(0, 0); 20 | for (int i = 0; i < maxTimes; i++) { 21 | set.add(sequence.nextId()); 22 | } 23 | System.out.println(maxTimes == set.size()); 24 | // Assert.assertEquals(maxTimes, set.size()); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/cn/izern/sequence/SequenceTest1.java: -------------------------------------------------------------------------------- 1 | package cn.izern.sequence; 2 | 3 | import org.junit.Test; 4 | 5 | import cn.izern.sequence.Sequence; 6 | 7 | public class SequenceTest1 { 8 | 9 | @Test 10 | public void name() { 11 | try { 12 | int times = 0, maxTimes = 1000; 13 | Sequence sequence = new Sequence(0, 0); 14 | for (int i = 0; i < maxTimes; i++) { 15 | long id = sequence.nextId(); 16 | if(id%2==0){ 17 | times++; 18 | } 19 | Thread.sleep(5); 20 | } 21 | System.out.println("偶数:" + times + ",奇数:" + (maxTimes - times) + "!"); 22 | } catch (Exception e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/cn/izern/sequence/ContiPerfTest.java: -------------------------------------------------------------------------------- 1 | package cn.izern.sequence; 2 | 3 | import org.databene.contiperf.PerfTest; 4 | import org.databene.contiperf.junit.ContiPerfRule; 5 | import org.junit.Rule; 6 | import org.junit.Test; 7 | 8 | import cn.izern.sequence.Sequence; 9 | 10 | /** 11 | * 性能测试 12 | * 13 | * @author lry 14 | */ 15 | public class ContiPerfTest { 16 | 17 | @Rule 18 | public ContiPerfRule i = new ContiPerfRule(); 19 | 20 | Sequence sequence = new Sequence(0, 0); 21 | 22 | @Test 23 | @PerfTest(invocations = 200000000, threads = 16) 24 | public void test1() throws Exception { 25 | sequence.nextId(); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /src/test/java/cn/izern/hibernate/id/Demo.java: -------------------------------------------------------------------------------- 1 | package cn.izern.hibernate.id; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | import org.hibernate.annotations.GenericGenerator; 11 | 12 | @Entity 13 | @Table(name = "demo") 14 | public class Demo implements Serializable{ 15 | 16 | /** 17 | * 18 | */ 19 | private static final long serialVersionUID = -2765770852213608028L; 20 | 21 | private Long id; 22 | 23 | // other 24 | 25 | @Id 26 | @GeneratedValue(generator = "idGenerator") 27 | @GenericGenerator(name = "idGenerator", strategy = "cn.izern.hibernate.id.IDWorkerGenerator") 28 | public Long getId() { 29 | return id; 30 | } 31 | 32 | public void setId(Long id) { 33 | this.id = id; 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/izern/hibernate/id/IDSequenceGenerator.java: -------------------------------------------------------------------------------- 1 | package cn.izern.hibernate.id; 2 | 3 | import java.io.Serializable; 4 | import java.util.Properties; 5 | 6 | import org.hibernate.HibernateException; 7 | import org.hibernate.MappingException; 8 | import org.hibernate.engine.spi.SessionImplementor; 9 | import org.hibernate.id.Configurable; 10 | import org.hibernate.id.IdentifierGenerator; 11 | import org.hibernate.service.ServiceRegistry; 12 | import org.hibernate.type.Type; 13 | 14 | import cn.izern.sequence.Sequence; 15 | 16 | /** 17 | * ID生成器,分布式Long型唯一ID,大小序列 18 | * @author zern 19 | * create on 2017年8月18日 20 | */ 21 | public class IDSequenceGenerator implements Configurable, IdentifierGenerator{ 22 | 23 | private Sequence sequence = new Sequence(); 24 | 25 | @Override 26 | public Serializable generate(SessionImplementor arg0, Object arg1) throws HibernateException { 27 | return sequence.nextId(); 28 | } 29 | 30 | @Override 31 | public void configure(Type arg0, Properties arg1, ServiceRegistry arg2) throws MappingException { 32 | // TODO Auto-generated method stub 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/cn/izern/sequence/SequenceTest.java: -------------------------------------------------------------------------------- 1 | package cn.izern.sequence; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import cn.izern.sequence.Sequence; 7 | 8 | public class SequenceTest { 9 | 10 | public static void main(String[] args) { 11 | Set set = new HashSet(); 12 | final Sequence idWorker1 = new Sequence(0, 0); 13 | final Sequence idWorker2 = new Sequence(1, 0); 14 | Thread t1 = new Thread(new IdWorkThread(set, idWorker1)); 15 | Thread t2 = new Thread(new IdWorkThread(set, idWorker2)); 16 | t1.setDaemon(true); 17 | t2.setDaemon(true); 18 | t1.start(); 19 | t2.start(); 20 | try { 21 | Thread.sleep(30000); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | 27 | static class IdWorkThread implements Runnable { 28 | private Set set; 29 | private Sequence idWorker; 30 | 31 | public IdWorkThread(Set set, Sequence idWorker) { 32 | this.set = set; 33 | this.idWorker = idWorker; 34 | } 35 | 36 | @Override 37 | public void run() { 38 | while (true) { 39 | long id = idWorker.nextId(); 40 | if (!set.add(id)) { 41 | System.out.println("duplicate:" + id); 42 | } 43 | } 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 分布式高效唯一ID生成器(sequence) 2 | 3 | 4 | 基于开源项目[sequence](https://git.oschina.net/yu120/sequence) 5 | 6 | 7 | ## 简介 8 | 高效GUID产生算法(sequence),基于Snowflake实现64位自增ID算法。 9 | 10 | Twitter-Snowflake算法产生的背景相当简单,为了满足Twitter每秒上万条消息的请求,每条消息都必须分配一条唯一的id,这些id还需要一些大致的顺序(方便客户端排序),并且在分布式系统中不同机器产生的id必须不同。 11 | 12 | 性能测试数据: 13 | 14 | ![性能测试结果](docs/perf.png) 15 | 16 | ## Snowflake算法核心 17 | 把时间戳,工作机器id,序列号组合在一起。 18 | 19 | ![Snowflake算法核心](docs/snowflake-64bit.jpg) 20 | 21 | 除了最高位bit标记为不可用以外,其余三组bit占位均可浮动,看具体的业务需求而定。默认情况下41bit的时间戳可以支持该算法使用到2082年,10bit的工作机器id可以支持1023台机器,序列号支持1毫秒产生4095个自增序列id。下文会具体分析。 22 | 23 | ## Snowflake – 时间戳 24 | 这里时间戳的细度是毫秒级,具体代码如下,建议使用64位linux系统机器,因为有vdso,gettimeofday()在用户态就可以完成操作,减少了进入内核态的损耗。 25 | 26 | ## Snowflake – 工作机器id 27 | 严格意义上来说这个bit段的使用可以是进程级,机器级的话你可以使用MAC地址来唯一标示工作机器,工作进程级可以使用IP+Path来区分工作进程。如果工作机器比较少,可以使用配置文件来设置这个id是一个不错的选择,如果机器过多配置文件的维护是一个灾难性的事情。 28 | 29 | ## Snowflake – 序列号 30 | 序列号就是一系列的自增id(多线程建议使用atomic),为了处理在同一毫秒内需要给多条消息分配id,若同一毫秒把序列号用完了,则“等待至下一毫秒”。 31 | 32 | # 获取 33 | ```xml 34 | 35 | cn.izern 36 | sequence 37 | ${version} 38 | 39 | ``` 40 | ## 使用 41 | ```java 42 | import cn.izern.sequence.Sequence; 43 | 44 | Sequence sequence = new Sequence(); 45 | sequence.nextId(); 46 | ``` 47 | 线程安全,生成唯一序列ID 48 | 49 | ## hibernate/jpa 使用Sequence作为ID生成方式 50 | 51 | ```java 52 | private Long id; 53 | 54 | // other 55 | 56 | @Id 57 | @GeneratedValue(generator = "idGenerator") 58 | @GenericGenerator(name = "idGenerator", strategy = "cn.izern.hibernate.id.IDSequenceGenerator") 59 | public Long getId() { 60 | return id; 61 | } 62 | 63 | public void setId(Long id) { 64 | this.id = id; 65 | } 66 | ``` -------------------------------------------------------------------------------- /src/main/java/cn/ms/sequence/SystemClock.java: -------------------------------------------------------------------------------- 1 | package cn.ms.sequence; 2 | 3 | import java.sql.Timestamp; 4 | import java.util.concurrent.Executors; 5 | import java.util.concurrent.ScheduledExecutorService; 6 | import java.util.concurrent.ThreadFactory; 7 | import java.util.concurrent.TimeUnit; 8 | import java.util.concurrent.atomic.AtomicLong; 9 | 10 | /** 11 | * 高并发场景下System.currentTimeMillis()的性能问题的优化 12 | * System.currentTimeMillis()的调用比new一个普通对象要耗时的多(具体耗时高出多少我还没测试过,有人说是100倍左右)

13 | * System.currentTimeMillis()之所以慢是因为去跟系统打了一次交道

14 | * 后台定时更新时钟,JVM退出时,线程自动回收

15 | * 10亿:43410,206,210.72815533980582%

16 | * 1亿:4699,29,162.0344827586207%

17 | * 1000万:480,12,40.0%

18 | * 100万:50,10,5.0%

19 | * @author lry 20 | */ 21 | public class SystemClock { 22 | 23 | private final long period; 24 | private final AtomicLong now; 25 | 26 | private SystemClock(long period) { 27 | this.period = period; 28 | this.now = new AtomicLong(System.currentTimeMillis()); 29 | scheduleClockUpdating(); 30 | } 31 | 32 | private static class InstanceHolder { 33 | public static final SystemClock INSTANCE = new SystemClock(1); 34 | } 35 | 36 | private static SystemClock instance() { 37 | return InstanceHolder.INSTANCE; 38 | } 39 | 40 | private void scheduleClockUpdating() { 41 | ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { 42 | public Thread newThread(Runnable runnable) { 43 | Thread thread = new Thread(runnable, "System Clock"); 44 | thread.setDaemon(true); 45 | return thread; 46 | } 47 | }); 48 | scheduler.scheduleAtFixedRate(new Runnable() { 49 | public void run() { 50 | now.set(System.currentTimeMillis()); 51 | } 52 | }, period, period, TimeUnit.MILLISECONDS); 53 | } 54 | 55 | private long currentTimeMillis() { 56 | return now.get(); 57 | } 58 | 59 | public static long now() { 60 | return instance().currentTimeMillis(); 61 | } 62 | 63 | public static String nowDate() { 64 | return new Timestamp(instance().currentTimeMillis()).toString(); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | cn.izern 5 | sequence 6 | 1.0.0 7 | jar 8 | 9 | sequence 10 | https://github.com/izern/sequence 11 | 2017 12 | 高效GUID产生算法(sequence),基于Snowflake实现64位自增ID算法。 13 | 14 | https://github.com/izern/sequence 15 | scm:https://github.com/izern/sequence.git 16 | 17 | 18 | 19 | zern 20 | jiaozi362330@gmail.com 21 | 22 | 23 | 24 | 25 | oss 26 | https://oss.sonatype.org/content/repositories/snapshots/ 27 | 28 | 29 | oss 30 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 31 | 32 | 33 | 34 | 35 | Apache 2 36 | http://www.apache.org/licenses/LICENSE-2.0.txt 37 | repo 38 | A business-friendly OSS license 39 | 40 | 41 | 42 | 43 | 4.7 44 | 5.0.12.Final 45 | UTF-8 46 | UTF-8 47 | 1.8 48 | 49 | 50 | 51 | 52 | org.hibernate 53 | hibernate-core 54 | ${hibernate.version} 55 | provided 56 | 57 | 58 | 59 | 60 | junit 61 | junit 62 | 4.7 63 | test 64 | 65 | 66 | 67 | org.databene 68 | contiperf 69 | 2.1.0 70 | test 71 | 72 | 73 | 74 | 75 | 76 | org.apache.maven.plugins 77 | maven-compiler-plugin 78 | 2.3.2 79 | 80 | ${java.version} 81 | ${java.version} 82 | ${project.build.sourceEncoding} 83 | true 84 | 85 | 86 | 87 | org.apache.maven.plugins 88 | maven-surefire-plugin 89 | 2.5 90 | 91 | true 92 | 93 | 94 | 95 | 96 | 97 | 98 | release 99 | 100 | 101 | 102 | 103 | org.apache.maven.plugins 104 | maven-source-plugin 105 | 2.2.1 106 | 107 | 108 | package 109 | 110 | jar-no-fork 111 | 112 | 113 | 114 | 115 | 116 | 117 | org.apache.maven.plugins 118 | maven-javadoc-plugin 119 | 2.9.1 120 | 121 | 122 | package 123 | 124 | jar 125 | 126 | 127 | 128 | 129 | 130 | 131 | org.apache.maven.plugins 132 | maven-gpg-plugin 133 | 1.5 134 | 135 | 136 | verify 137 | 138 | sign 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | oss 148 | https://oss.sonatype.org/content/repositories/snapshots/ 149 | 150 | 151 | oss 152 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 153 | 154 | 155 | 156 | 157 | -------------------------------------------------------------------------------- /src/main/java/cn/izern/sequence/Sequence.java: -------------------------------------------------------------------------------- 1 | package cn.izern.sequence; 2 | 3 | import java.lang.management.ManagementFactory; 4 | import java.net.InetAddress; 5 | import java.net.NetworkInterface; 6 | 7 | import cn.ms.sequence.SystemClock; 8 | 9 | /** 10 | *

11 | * 分布式高效有序ID生产黑科技(sequence)
12 | * 优化开源项目:http://git.oschina.net/yu120/sequence 13 | *

14 | * 自定义生成全局唯一ID 15 | * @author zern 16 | * create on 2017年8月18日 17 | */ 18 | public class Sequence { 19 | 20 | /** 开始时间截 */ 21 | private final long twepoch = 1288834974657L; 22 | /** 机器id所占的位数 */ 23 | private final long workerIdBits = 5L; 24 | /** 数据标识id所占的位数 */ 25 | private final long datacenterIdBits = 5L; 26 | /** 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ 27 | private final long maxWorkerId = -1L ^ (-1L << workerIdBits); 28 | /** 支持的最大数据标识id,结果是31 */ 29 | private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); 30 | /** 序列在id中占的位数 */ 31 | private final long sequenceBits = 12L; 32 | /** 机器ID向左移12位 */ 33 | private final long workerIdShift = sequenceBits; 34 | /** 数据标识id向左移17位(12+5) */ 35 | private final long datacenterIdShift = sequenceBits + workerIdBits; 36 | /** 时间截向左移22位(5+5+12) */ 37 | private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; 38 | /** 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ 39 | private final long sequenceMask = -1L ^ (-1L << sequenceBits); 40 | 41 | /** 工作机器ID(0~31) */ 42 | private long workerId; 43 | /** 数据中心ID(0~31) */ 44 | private long datacenterId; 45 | /** 毫秒内序列(0~4095) */ 46 | private long sequence = 0L; 47 | /** 上次生成ID的时间截 */ 48 | private long lastTimestamp = -1L; 49 | 50 | 51 | public Sequence() { 52 | datacenterId = getDatacenterId(maxDatacenterId); 53 | workerId = getMaxWorkerId(datacenterId, maxWorkerId); 54 | } 55 | 56 | public Sequence(long workerId, long datacenterId) { 57 | if (workerId > maxWorkerId || workerId < 0) { 58 | throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); 59 | } 60 | 61 | if (datacenterId > maxDatacenterId || datacenterId < 0) { 62 | throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); 63 | } 64 | 65 | this.workerId = workerId; 66 | this.datacenterId = datacenterId; 67 | } 68 | 69 | 70 | /** 71 | * 获取 maxWorkerId 72 | * @param datacenterId 数据中心id 73 | * @param maxWorkerId 机器id 74 | * @return maxWorkerId 75 | */ 76 | protected static long getMaxWorkerId(long datacenterId, long maxWorkerId) { 77 | StringBuilder mpid = new StringBuilder(); 78 | mpid.append(datacenterId); 79 | String name = ManagementFactory.getRuntimeMXBean().getName(); 80 | if (name != null && "".equals(name)) { 81 | // GET jvmPid 82 | mpid.append(name.split("@")[0]); 83 | } 84 | //MAC + PID 的 hashcode 获取16个低位 85 | return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1); 86 | } 87 | 88 | /** 89 | *

90 | * 数据标识id部分 91 | *

92 | * @param maxDatacenterId 93 | * @return 94 | */ 95 | protected static long getDatacenterId(long maxDatacenterId) { 96 | long id = 0L; 97 | try { 98 | InetAddress ip = InetAddress.getLocalHost(); 99 | NetworkInterface network = NetworkInterface.getByInetAddress(ip); 100 | if (network == null) { 101 | id = 1L; 102 | } else { 103 | byte[] mac = network.getHardwareAddress(); 104 | if (null != mac) { 105 | id = ((0x000000FF & (long) mac[mac.length - 1]) | (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6; 106 | id = id % (maxDatacenterId + 1); 107 | } 108 | } 109 | } catch (Exception e) { 110 | System.err.println(" getDatacenterId: " + e.getMessage()); 111 | } 112 | return id; 113 | } 114 | 115 | /** 116 | * 获得下一个ID (该方法是线程安全的) 117 | * 118 | * @return nextId 119 | */ 120 | public synchronized long nextId() { 121 | long timestamp = timeGen(); 122 | 123 | // 如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 124 | if (timestamp < lastTimestamp) {// 闰秒 125 | long offset = lastTimestamp - timestamp; 126 | if (offset <= 5) { 127 | try { 128 | wait(offset << 1); 129 | timestamp = timeGen(); 130 | if (timestamp < lastTimestamp) { 131 | throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", offset)); 132 | } 133 | } catch (Exception e) { 134 | throw new RuntimeException(e); 135 | } 136 | } else { 137 | throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", offset)); 138 | } 139 | } 140 | 141 | //$NON-NLS-解决跨毫秒生成ID序列号始终为偶数的缺陷$ 142 | // 如果是同一时间生成的,则进行毫秒内序列 143 | if (lastTimestamp == timestamp) { 144 | sequence = (sequence + 1) & sequenceMask; 145 | // 毫秒内序列溢出 146 | if (sequence == 0) { 147 | // 阻塞到下一个毫秒,获得新的时间戳 148 | timestamp = tilNextMillis(lastTimestamp); 149 | } 150 | } else {// 时间戳改变,毫秒内序列重置 151 | sequence = 0L; 152 | } 153 | /** 154 | // 如果是同一时间生成的,则进行毫秒内序列 155 | if (lastTimestamp == timestamp) { 156 | long old = sequence; 157 | sequence = (sequence + 1) & sequenceMask; 158 | // 毫秒内序列溢出 159 | if (sequence == old) { 160 | // 阻塞到下一个毫秒,获得新的时间戳 161 | timestamp = tilNextMillis(lastTimestamp); 162 | } 163 | } else {// 时间戳改变,毫秒内序列重置 164 | sequence = ThreadLocalRandom.current().nextLong(0, 2); 165 | } 166 | **/ 167 | 168 | // 上次生成ID的时间截 169 | lastTimestamp = timestamp; 170 | 171 | // 移位并通过或运算拼到一起组成64位的ID 172 | return ((timestamp - twepoch) << timestampLeftShift) // 173 | | (datacenterId << datacenterIdShift) // 174 | | (workerId << workerIdShift) // 175 | | sequence; 176 | } 177 | 178 | /** 179 | * 阻塞到下一个毫秒,直到获得新的时间戳 180 | * 181 | * @param lastTimestamp 上次生成ID的时间截 182 | * @return 当前时间戳 183 | */ 184 | protected long tilNextMillis(long lastTimestamp) { 185 | long timestamp = timeGen(); 186 | while (timestamp <= lastTimestamp) { 187 | timestamp = timeGen(); 188 | } 189 | 190 | return timestamp; 191 | } 192 | 193 | /** 194 | * 返回以毫秒为单位的当前时间 195 | * 196 | * @return 当前时间(毫秒) 197 | */ 198 | protected long timeGen() { 199 | return SystemClock.now(); 200 | } 201 | 202 | } 203 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------